js综合能力提升 继承prototype

来源:互联网 发布:薛之谦主持的网络综艺 编辑:程序博客网 时间:2024/04/30 09:49

prototype用来做继承机制

    //构造函数 用来构造对象  function Cat(name){    this.name = name;  }    //共享属性 属性名为species  Cat.prototype = { species : '猫科' };    //使用构造函数 构造两个对象 共享species属性  var catA = new Cat('小林');  var catB = new Cat('小森');  alert(catA.species); // 猫科  alert(catB.species); // 猫科    //修改后属性改变,共享的属性也改变    Cat.prototype = { species : '犬科' };    alert(catA.species); // 犬科      alert(catB.species); // 犬科


 

0 0