当前位置: 移动技术网 > IT编程>软件设计>设计模式 > Javascript继承3:将优点为我所有----组合式继承

Javascript继承3:将优点为我所有----组合式继承

2018年10月03日  | 移动技术网IT编程  | 我要评论
//声明父类
function parentclass(name){
    //值类型公有属性
    this.name = name
    //引用类型公有属性
    this.books = ['html']
}
//父类型原型公有方法
parentclass.prototype.getname = function(){
    console.log(this.name);
}
//声明子类
function childclass(name,id){
    //构造函数式继承父类name属性
    parentclass.call(this,name);
    //子类中新增公有属性
    this.id = id;
}
// 类式继承  子类原型继承父类
childclass.prototype = new parentclass();
// 子类原型方法
childclass.prototype.getid = function(){
    console.log(this.id);
}

var child1 = new childclass('css',1)
child1.books.push('图解css');
console.log(child1.books) // ['html','图解css']
child1.getname()          // css
child1.getid()            // 1


var child2 = new childclass('javascript',2)
console.log(child2.books) // ['html']
child2.getname()          // javascript
chil2.getid()             // 2

设计模式中的经典笔录

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网