JavaScript基础之原型链

原文:https://www.jianshu.com/p/b745c5481fab?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=qqhttps://home.firefoxchina.cn

1.Function对象和Object对象的原型指向关系:

1> function定义的函数对象的proto属性指向Function对象的prototype对象。
2> 非function定义的对象的proto属性指向创建它的构造函数的prototype对象。
3> Object的prototype对象的proto指向null。
4> Function的prototype对象的proto指向Object对象的prototype对象。

测试代码如下:

console.log(Object.prototype);
console.log(Object.prototype.constructor === Object);//true
console.log(Object.prototype.__proto__ === null);//true 
console.log(Object.__proto__ === Function.prototype);//true 
console.log(Function.prototype); 
console.log(Function.prototype.constructor === Function);//true
console.log(Function.prototype.__proto__ === Object.prototype);//true 
console.log(Function.__proto__ === Function.prototype);//true

2.根据组合继承理解原型链,代码如下:

//父类 
function Person(name,age){ 
    if(this instanceof Person){ 
        this.name = name; this.age = age; 
    }else { 
        Person.call(this,name,age); 
    } 
} 

Person.prototype.say = function(){ 
    console.log('我叫' + this.name +',今年' + this.age + '岁了!') 
} 

//子类 
function Student(name,age,gride){ 
    if(this instanceof Student){ 
        Person.call(this,name,age); 
    }else { 
        Student.call(this,name,age,gride); 
    } 
} 

Student.prototype = new Person(); 
Student.prototype.constructor = Student; 

Student.prototype.say = function(){ 
    console.log('我叫' + this.name +',今年' + this.age + '岁了,考试考了' + this.gride + '分!'); 
} 

var stu = new Student('chping',23,100); 
stu.say();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值