js 原型链,之前不太懂,在网上找了很多年文章,研究了一下,自己理解的
js有两个特殊对象,Object 和 Function ,他们都是构造函数,用于生成对象, 其中Object.prototype 是 所有对象的祖先,也就是头,
Function.prototype 是所有函数的原型,包括构造函数,
在js 中 每个对象都有个prototype 属性,也就是 对象与原型之间的链接 标记为__proto__ 原型链
对象的原型的__proto__ 可以自由指定指向,默认指向Object.prototype;而 Object.prototype的__proto__ 是 null
例如(例子摘抄网络)
var a = {
x:10
}
var b = {
y:20,
__proto__:a
}
var c = {
y:30,
__proto__:a
}
console.log(c.__proto__); // Object {x: 10}
console.log(b.__proto__); //Object {x: 10}
console.log(c.x); //10
console.log(a.x); //10
console.log(b.x); //10
Function 原型的__proto__指向Function.prototype
Function.prototype 的 原型链__proto__指向Object.prototype
构造函数的__proto__ 指向 Function.prototype
Object的__proto__ 指向 Function.prototype Function.prototype
function Foo(x){
this.x = x;
}
var foo = new Foo('40');
Foo.prototype.z='50';
console.log(Foo.__proto__);//function() {}
console.log(foo.__proto__);//Object {z: "50"}
如果 不完整 请指出 谢谢