js中prototype和_proto_的区别

prototype是函数的属性(Function特有的),_proto_属于对象内部的属性(Function内也有,可称为隐式原型,一个对象的隐式原型指向构造该对象的构造函数的原型),所有构造器/函数的_proto_都指向Function.prototype,它是一个空函数(Empty function)
如:
Function.proto=Function.prototype//true
Number.proto
= Function.prototyp //true
String.proto=== Function.prototyp //true

var arr = [1,2]
arr.proto===Array.prototype//true

prototype属性继承自哪里呢(是一个Object对象)
function Person(name) {
this.name = name
}
// 修改原型
Person.prototype.say= function() {}
var p = new Person(‘jet’)
console.log(p.proto=== Person.prototype) // true
console.log(p.proto=== p.constructor.prototype) // true

另外_proto_属于浏览器内置属性不是标准方式,在有些浏览器里面可能会报错,可用标准的方法是Object.getPrototypeOf()获取;

猜你喜欢

转载自blog.csdn.net/smlljet/article/details/89878940