- //声明父类
- 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