原型中的方法可以相互访问

function Animal(name,age){
    this,name = name;
    this.age = age;
}
//原型中添加方法
Animal.prototype.eat = function(){
    console.log("动物吃东西");
    this.play();
};
Animal.prototype.play = function(){
    console.log("玩球");
    this.sleep();
};
Animal.prototype.sleep = function(){
    console.log("睡觉了");
};
//实例化对象
var dog = new Animal("小明",20);
dog.eat();
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容