函数和原型链

这篇博客深入探讨了JavaScript中的原型链、构造函数和实例化过程,以及ES6中的类继承。通过示例展示了如何创建和使用构造函数以及类,包括子类继承父类的方法。还提到了递归的概念,并给出一个简单的递归函数示例。此外,文章也涵盖了ES5中的面向对象实现,通过函数构造器展示了属性和方法的定义与调用。
摘要由CSDN通过智能技术生成

型链

new __ proto __ constructor

构造函数<=========>实例化对 --------------------->原型对象<===========>构造函数

constructor prototype

__ proto __ constructor __ proto __

原型对象-------------->Object 原型对象<=======>Object构造函数------------>Null

prototype

ES6继承

Class类继承

// 父类 Person
class Person {
    constructor(uname, age) {
​
        //属性
        this.uname = uname;
        this.age = age
    }
​
    //方法
    say() {
        return `我是${this.uname},我年龄${this.age}岁`
    }
}
let arr = Person('刘德华',38)
console.log(arr.say())
​
​
//子类Student
class Student extends Person {
    constructor(uname, age) {
        // this.uname = uname;
        // this.age = age;
        super(uname, age)
    }
​
}
​
​
var s1 = new Student("小红", 18)
console.log(s1.say())

递归

概念:在一个函数的内部自调用,有退出条件

举例:

var n = 1;
function fun() {
    if (n > 6) return;
    console.log("我喜欢翘臀")
    n++
    fun()
}
​
fun()

面向对象

ES5实现

function Star(uname, height, weight) {
    this.uname = uname;
    this.height = height;
    this.weight = weight
​
    this.say = function () {
        console.log(`我是${this.uname},我身高${this.height}厘米,体重${this.weight}公斤`)
    }
}
​
var ldh = new Star("刘德华", 174, 64)
ldh.say()
​
var lyf = new Star("刘亦菲", 170, 80)
lyf.say()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值