JavaScript的原型对象与原型链

prototype和proto的区别

image

vara = {};

console.log(a.prototype);  //undefinedconsole.log(a.__proto__);//Object {}varb =function(){}

console.log(b.prototype);  //b {}console.log(b.__proto__);//function() {}

image

/*1、字面量方式*/

var a = {};

console.log(a.__proto__);  //Object {}

console.log(a.__proto__ === a.constructor.prototype); //true

/*2、构造器方式*/

var A = function(){};

var a = new A();

console.log(a.__proto__); //A {}

console.log(a.__proto__ === a.constructor.prototype); //true

/*3、Object.create()方式*/

var a1 = {a:1}

var a2 = Object.create(a1);

console.log(a2.__proto__); //Object {a: 1}

console.log(a.__proto__ === a.constructor.prototype); //false(此处即为图1中的例外情况)

image

varA =function(){};vara =new A();

console.log(a.__proto__); //A {}(即构造器function A 的原型对象)console.log(a.__proto__.__proto__);//Object {}(即构造器function Object 的原型对象)console.log(a.__proto__.__proto__.__proto__);//null

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容