代码改变世界

prototype

2016-10-10 14:53  john老张  阅读(126)  评论(0)    收藏  举报

摘自--阮一峰BLOG

 

  function Cat(name,color){

    this.name = name;

    this.color = color;

  }

  Cat.prototype.type = "猫科动物";

  Cat.prototype.eat = function(){alert("吃老鼠")};

 

var cat1 = new Cat("大毛","黄色");

var cat2 = new Cat("二毛","黑色");

  alert(cat1.type); // 猫科动物

  cat1.eat(); // 吃老鼠