prototype

作者: 葶寳寳 | 来源:发表于2017-04-06 20:31 被阅读0次

官方描述: 函数对象被创建时,Function构造器产生的函数对象会运行这样一些代码:

 this.prototype = {constructor: this};

1.prototype是函数对象的属性,普通对象没有该属性:

普通对象没有prototype属性.png 函数对象prototype.png

2.函数对象和普通对象都有__proto__属性,它指向该对象原型的prototype

构造函数、实例与原型关系图

构造函数、实例与原型关系图.png
function Person() {}
let person = new Person();

// 在所有实现中都无法访问,以下的访问方式已被弃用。
console.log(Person.__proto__ === Function.prototype);
console.log(person.__proto__ === Person.prototype);

// 判断__proto__的指向
console.log(Object.prototype.isPrototypeOf(person)); 
console.log(Object.getPrototypeOf(person) === Func.prototype); // 推介

// 判断Func原型的constructor指向
console.log(Person.prototype.constructor === Person)

// Person和person中的constructor属性指向它的构造函数
console.log(person.constructor === Person);
console.log(Person.constructor === Function);

相关文章

网友评论

      本文标题:prototype

      本文链接:https://www.haomeiwen.com/subject/nsjngttx.html