在群里讨论JavaScript,然后得出了这几种写法,感觉是层层递进,想了想,最后选择发布成文章

({
    baby : '大头儿子',
    dady : '小头爸爸',
    run : function() {
        console.log(this.dady+ this.callbaby() + this.eat());
    },
    callbaby : function() {
        return '叫'+this.baby+'去';
    },
    eat : function() {
        return '吃饭';
    },
}).run();


function GoFood() {
    this.baby = '大头儿子';
    this.dady = '小头爸爸';
};

GoFood.prototype.run = function() {
        console.log(this.dady+ this.callbaby() + this.eat());
    };
GoFood.prototype.callbaby = function() {
        return '叫'+this.baby+'去';
    };
GoFood.prototype.eat = function() {
        return '吃饭';
    };

var GoFood1 = new GoFood();
GoFood1.run();

var eatObj = {
    baby : '大头儿子',
    dady : '小头爸爸',
    run : function(callback) {
        callback = (typeof callback == 'function' ) ? callback : function() {};
        console.log(this.dady+ this.callbaby() + this.eat()+callback());
    },
    callbaby : function() {
        return '叫'+this.baby+'去';
    },
    eat : function() {
        return '吃饭';
    },
}

var eatObj1 = Object.create(eatObj);
eatObj1.run(function() {
    return ',吃完了背小头儿子回去';
});


function Person(name){
this.name=name;
}
Person.prototype={
 getHand:function(person){
 console.log(this.name+"正在牵着"+person.name+"的手");
 },
  eat:function(rice){
  console.log(this.name+"正在吃"+rice.name+"....");
 }
}

function Rice(name){
 this.name=name;
}
//***************给所有对象扩展一个继承的方法(extends继承)**********
Object.prototype.extends=function(func){  
    for(var p in func.prototype){
    this.prototype[p]=func.prototype[p]; 
    }

}
//father 
function Father(name){
 this.name=name;
}
Father.extends(Person);  //爸爸要继承人的功能
Father.prototype.carry=function(person){
    console.log(this.name+"正在背着"+person.name+"......");
    }

//小孩
function Children(name){
  this.name=name;
}
Children.extends(Person);//小孩继承人的功能

var x=new Father("小头爸爸");
var d=new Children("大头儿子");
x.getHand(d);
var  p1=new Rice("水饺");
var p2=new Rice("炒粉");
x.eat(p1);
d.eat(p2);
x.carry(d);

kumfo
6.7k 声望4.1k 粉丝

程序生存法则: