<script>
//在inner函数内部定义了两个变量age和colors,在outer内部定义了一个变量name
// 在inner和outer原型上分别定义了sayAge和sayName方法
function inner(age) {
this.age = age;
this.colors = ["pink", "red"];
}
inner.prototype.sayAge = function () {
return this.age;
}
function outer(age, name) {
inner.call(this, age);
this.name = name;
}
// outer内部使用call方法调用inner作用域变换
outer.prototype = new inner();
outer.prototype.sayName = function () {
return this.name;
}
// 再利用原型链,使得outer能够使用以上定义变量和方法
var firOut = new outer(18, "小王");
firOut.colors.push("orange");
var secOut = new outer(20, "老王");
console.log(firOut);
console.log(secOut);
// 这样就保留了实例各自的属性值,也可以使用相同的方法
</script>


被折叠的 条评论
为什么被折叠?



