function Base(){
}

Base.prototype = {x:10, y:[]}

function A(){}
A.prototype = new Base();
var a = new A();
a.y.push("first");
a.x = 9;

console.log(a.x);
console.log(a.y);


function B(){}
B.prototype = new Base();
var b = new B();
b.y.push("second");
b.x = 11;

console.log(a.x);
console.log(a.y);

允许结果如下:

9
["first"]
9
["first", "second"]

注意这里,原型中有基本数据类型和对象类型(比如数组)的时候行为不一致,需要理解引用类型的概念。

相关文章:

  • 2021-12-24
  • 2022-12-23
  • 2021-12-19
  • 2021-09-10
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-29
  • 2021-07-13
  • 2022-12-23
  • 2021-04-09
  • 2022-12-23
  • 2021-11-14
  • 2021-05-20
相关资源
相似解决方案