(本文未经许可禁止转载)

           

源代码

function foo(){}
foo.prototype = {
  foo_prop: "foo val"
};
function bar(){}
var proto = new foo;
proto.bar_prop = "bar val";
bar.prototype = proto;
var inst = new bar;
console.log(inst.foo_prop);
console.log(inst.bar_prop);

结果:

foo val
bar val

此时的原型链为:

inst{}-->proto{bar_prop: 'bar val'}-->foo.prototype{foo_prop:'foo val'}-->Object.prototype{...}-->null

       

(本文未经许可禁止转载)

   

发表评论