javascript 继承

定义构造器并扩充它的原型

var Mammal = function(name){  this.name = name; };

Mammal.prototype.getName = function(){  return this.name; }; Mammal.prototype.says = function(){  return this.saying||""; };

var Cat = function(name){  this.name = name;  this.saying = "cat"; };

Cat.prototype = new Mammal();

Cat.prototype.sayCatName = function(){  return this.name; };

var cat = new Cat("bb");

var name = cat.getName();//bb

cat.says();//cat

 

 

posted @ 2014-03-07 08:34  菠萝君  阅读(102)  评论(0)    收藏  举报