美文网首页
JS prototype 探索

JS prototype 探索

作者: jtsky | 来源:发表于2017-09-08 11:40 被阅读8次

source

function Car() {}

var car1 = new Car();
var car2 = Object.create(car1);
var car3 = Object.create(Car);

console.log('car1===>',car1)
console.log('car2===>',car2)
console.log('car3===>',car3)
console.log('Car====>',Car)
console.log('           ')
console.log('car1.prototype===>',Object.getPrototypeOf(car1))
console.log('car2.prototype===>',Object.getPrototypeOf(car2))
console.log('car3.prototype===>',Object.getPrototypeOf(car3))
console.log('Car.prototype====>',Car.prototype)
console.log('           ')
console.log('car1.constructor============>',car1.constructor)
console.log('car2.constructor============>',car2.constructor)
console.log('car3.constructor============>',car3.constructor)
console.log('Car.prototype.constructor===>',Car.prototype.constructor)
console.log('Car.constructor=============>',Car.constructor)

console

car1===> Car {}
car2===> Car {}
car3===> Function {}
Car====> function Car() {}

car1.prototype===> Car {}
car2.prototype===> Car {}
car3.prototype===> function Car() {}
Car.prototype====> Car {}

car1.constructor============> function Car() {}
car2.constructor============> function Car() {}
car3.constructor============> function Function() { [native code] }
Car.prototype.constructor===> function Car() {}
Car.constructor=============> function Function() { [native code] }

结论

Object.create()

image.png

new 操作符

image.png

1、对象与函数:car1、car2的性质都是Car对象,car3是Function对象,而Car属于函数。
2、原型:car1、car2、Car的原型都是Car对象本身,这也符合原型链的规则,而car3的原型就是Car函数,符合Object.create()的定义
3、构造函数:car1、car2、Car原型的构造函数都是函数对象,和原型同理。而car3和Car的构造函数则属于函数本身的构造函数

相关文章

网友评论

      本文标题:JS prototype 探索

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