<script type="text/javascript">
//声明父类
function SuperClass () {
this.superValue = true;
}
// 父类原型
SuperClass.prototype.getSuperValue = function () {
return this.superValue;
}
// 声明子类
function SubClass () {
this.subValue = false;
}
// 继承父类
SubClass.prototype = new SuperClass();
SubClass.prototype.getSubValue = function () {
return this.subValue;
}
// 对子类实例化
var instance = new SubClass();
console.log(instance.getSuperValue()); //true
console.log(instance.getSubValue()); //false
</script>
注意点
在实现subClass的继承的时候是将superClass的实例赋值给了subClass的原型prototype,所以SubClass.prototype 继承了 SuperClass
缺点
<script type="text/javascript">
//声明父类
function SuperClass () {
this.books = ['js'];
}
function SubClass () {
}
// 继承父类
SubClass.prototype = new SuperClass();
var instance1 = new SubClass();
var instance2 = new SubClass();
console.log(instance2.books);
instance1.books.push('CSS');
console.log(instance2.books);
</script>
instance1的修改会改变了instance2的books属性
<script type="text/javascript">
// 声明父类
function SuperClass(id) {
this.books = ['JavaScript','html','css'];
this.id = id;
}
SuperClass.prototype.showBooks = function(){
console.log(this.books);
}
// 声明子类
function SubClass(id){
// 继承父类
SuperClass.call(this,id);
}
// 创建实例
var instance1 = new SubClass(10);
var instance2 = new SubClass(11);
instance1.books.push('设计模式');
console.log(instance1.books);
console.log(instance1.id);
console.log(instance2.books);
console.log(instance2.id);
</script>
注意
类式继承是通过子类的原型prototype对父类的实例化来实现的
构造函数式继承是通过在子类的构造函数作用环境中执行一次父类的构造函数实现的(call)
将二者结合起来就出现了组合式继承
<script type="text/javascript">
// 声明父类
function SuperClass(name) {
this.books = ['JavaScript','html','css'];
this.name = name;
}
SuperClass.prototype.getName = function(){
console.log(this.name);
}
// 声明子类
function SubClass(name,time){
// 继承父类
SuperClass.call(this,name);
this.time = time;
}
SubClass.prototype = new SuperClass()
SubClass.prototype.getTime = function(){
console.log(this.time);
}
// 创建实例
var instance1 = new SubClass('js book',2019);
instance1.books.push('设计模式');
console.log(instance1.books);
instance1.getName();
instance1.getTime()
var instance2 = new SubClass('css book',2020);
console.log(instance2.books);
instance2.getName();
instance2.getTime();
</script>
<script type="text/javascript">
function inheritObject(o){
function F(){};
F.prototype = o;
return new F();
}
var book = {
name:'js book',
alikeBook: ['css book','html book']
}
var newBook = inheritObject(book);
newBook.name = 'ajax book';
newBook.alikeBook.push('xml book');
var otherBook = inheritObject(book);
otherBook.name = 'flash book';
otherBook.alikeBook.push('as book');
console.log(newBook.name);
console.log(newBook.alikeBook);
console.log(otherBook.name);
console.log(otherBook.alikeBook);
console.log(book.name);
console.log(book.alikeBook);
</script>
注意
跟类式继承一样,由于是通过prototype从父类继承来的,因此父类对象book中的值类型的属性被复制,会被共用。
<script type="text/javascript">
function inheritObject(o){
function F(){};
F.prototype = o;
return new F();
}
function inheritPrototype(subClass,superClass){
// 复制父类的原型副本保存在变量中
var p = inheritObject(superClass.prototype);
// 修正因为重写子类原型导致的子类的constructor属性被修改
p.constructor = subClass;
subClass.prototype = p;
}
// 定义父类
function SuperClass(name){
this.name = name;
this.colors = ['red','blue','green']
}
SuperClass.prototype.getName = function(){
console.log(this.name);
}
// 定义子类
function SubClass(name,time){
// 构造函数式继承
SuperClass.call(this,name);
this.time = time;
}
// 寄生式继承父类原型
inheritPrototype(SubClass,SuperClass);
SubClass.prototype.getTime = function(){
console.log(this.time);
}
// 实例化
var instance1 = new SubClass('js book',2019);
var instance2 = new SubClass('css book',2020);
instance1.colors.push('black');
console.log(instance1.colors);
console.log(instance2.colors);
instance2.getName();
instance1.getTime();
</script>