javascript有继承。javascript继承是一种允许我们在已有类的基础上创建新类的机制,继承能够为子类提供灵活性,可以重用父类的方法和变量,可以利用原型链、构造函数来实现继承。
本教程操作环境:windows10系统、javascript1.8.5版、dell g3电脑。
javascript有没有继承javascript有继承。
javascript继承是一种允许我们在已有类的基础上创建新类的机制;它为子类提供了灵活性,可以重用父类的方法和变量。继承的过程,就是从一般到特殊的过程。
javascript如何实现继承?
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"
ecmascript5通过新增object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。
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);}
相关推荐:javascript学习教程
以上就是javascript有没有继承的详细内容。
