js实现继承的6种方式
来源:互联网 发布:阿里云备案承诺书下载 编辑:程序博客网 时间:2024/06/05 05:43
1.原型链
基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。
构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
function SuperType() { this.property = true;}SuperType.prototype.getSuperValue = function() { return this.property;}function subType() { this.property = false;}//继承了SuperTypeSubType.prototype = new SuperType();SubType.prototype.getSubValue = function (){ return this.property;}var instance = new SubType();console.log(instance.getSuperValue());//true
2.借用构造函数
基本思想:在子类型构造函数的内部调用父类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。
function SuperType() { this.colors = ["red","blue","green"];}function SubType() { SuperType.call(this);//继承了SuperType}var instance1 = new SubType();instance1.colors.push("black");console.log(instance1.colors);//"red","blue","green","black"var instance2 = new SubType();console.log(instance2.colors);//"red","blue","green"
3.组合继承
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。
function SuperType(name) {this.name = name;this.colors = ["red","blue","green"];}SuperType.prototype.sayName = function() {console.log(this.name);}function SubType(name, age) {SuperType.call(this,name);//继承属性this.age = age;}//继承方法SubType.prototype = new SuperType();Subtype.prototype.constructor = Subtype;Subtype.prototype.sayAge = function() {console.log(this.age);}var instance1 = new SubType("EvanChen",18);instance1.colors.push("black");consol.log(instance1.colors);//"red","blue","green","black"instance1.sayName();//"EvanChen"instance1.sayAge();//18var instance2 = new SubType("EvanChen666",20);console.log(instance2.colors);//"red","blue","green"instance2.sayName();//"EvanChen666"instance2.sayAge();//20
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。
原型式继承的思想可用以下函数来说明:
function object(o) {function F(){}F.prototype = o;return new F();}
例子:var person = {name:"EvanChen",friends:["Shelby","Court","Van"];};var anotherPerson = object(person);anotherPerson.name = "Greg";anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);yetAnotherPerson.name = "Linda";yetAnotherPerson.friends.push("Barbie");console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
var person = {name:"EvanChen",friends:["Shelby","Court","Van"];};
var anotherPerson = Object.create(person);anotherPerson.name = "Greg";anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);yetAnotherPerson.name = "Linda";yetAnotherPerson.friends.push("Barbie");console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
5.寄生式继承
基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。
function createAnother(original) {var clone = object(original);clone.sayHi = function () {alert("hi");};return clone;}var person = {name:"EvanChen",friends:["Shelby","Court","Van"];};var anotherPerson = createAnother(person);anotherPerson.sayHi();///"hi"
6.寄生组合式继承
基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法
其基本模型如下所示:
function inheritProperty(subType, superType) {var prototype = object(superType.prototype);//创建对象prototype.constructor = subType;//增强对象subType.prototype = prototype;//指定对象}例子:
function SuperType(name){this.name = name;this.colors = ["red","blue","green"];}SuperType.prototype.sayName = function (){alert(this.name);};function SubType(name,age){SuperType.call(this,name);this.age = age;}inheritProperty(SubType,SuperType);SubType.prototype.sayAge = function() {alert(this.age);}
阅读全文
0 0
- js实现继承的6种方式
- js实现继承的5种方式
- js实现继承的5种方式
- js实现继承的5种方式
- js实现继承的几种方式
- js实现继承的5种方式
- js实现继承的5种方式
- js实现继承的5种方式
- js实现继承的5种方式
- js实现继承的三种方式
- js实现继承的几种方式
- js实现继承的5种方式
- JS实现继承的几种方式
- JS继承的实现几种方式
- JS实现继承的几种方式
- js实现继承的几种方式
- JS实现继承的几种方式
- JS实现继承的几种方式
- 学生党的思维导图
- bzoj2754:[SCOI2012]喵星球上的点名 (后缀数组+离线+树状数组)
- -all_load和-force_load 的区别
- Linux网络编程-tcp缓存设置
- 高考估分查分选志愿一键搞定_支付宝又操办了件人生大事
- js实现继承的6种方式
- Java内存区域和内存溢出异常之一
- Spring的ApplicationListener及Spring容器事件使用实例
- gocron
- pthread_create函数的详细讲解(包括向线程函数传递参数详解)
- 说说我是怎么封装使用Retrofi+Rxjava的
- Java socket通讯
- ES6的学习笔记
- php thinkphp 设置session的有效时间