SOURCE

var Pet = function(name, legs) {
    this.name = name;
    this.legs = legs;
}
//创建一个方法可以展示Pet的名字和手的数量
Pet.prototype.getDetails = function () {
    return this.name + ' has ' + this.legs + ' legs '
}

var Cat = function (name) {
    Pet.call(this, name, 4)
    // call the parent object's constructor
}

//This line performs the inheritance from Pet.
//这行执行来自Pet的继承
Cat.prototype = new Pet()

//用一个action方法增强Cat类
Cat.prototype.action = function () {
    return 'Catch a bird'
}

var petCat = new Cat('Felix');

var details = petCat.getDetails();
var action = petCat.action();
petCat.name = "new_Felix";
petCat.legs = 3;//能修改成功
console.log(petCat.getDetails())

// 无prototype版

var pet = function(name,legs){
     var that = {
         name:name,
         getDetails: function(){
             return that.name + ' has ' + legs + ' legs '
         }
     }
     return that;
 }

 var cat = function(name){
     var that = pet(name, 4);
     that.action = function(){
         return 'Catch a bird';
     }
     return that
 }

 var petCat2 = cat('Felix2');
 details = petCat2.getDetails();
 action = petCat2.action();
 petCat2.name = 'new_Felix2';
 petCat2.legs = 7;
 //真正的legs值安全的保存在pet的getDetails闭包内部
 console.log(petCat2.getDetails())






console 命令行工具 X clear

                    
>
console