国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript面向對象之繼承

2019-11-06 06:37:15
字體:
來源:轉載
供稿:網友

1.      原型鏈繼承:

function SuperType(){	this.PRoperty = true;}SuperType.prototype.getSuperValue = function (){	return this.property;}function SubType(){	this.subproperty = false;}SubType.prototype = new SuperType();SubType.prototype.getSubValue = function (){	return this.subproperty;}var instance = new SubType();alert(instance.getSuperValue());alert(instance instanceof Object);alert(instance instanceof SuperType);alert(instance instanceof SubType);alert(Object.prototype.isPrototypeOf(instance));alert(SuperType.prototype.isPrototypeOf(instance));alert(SubType.prototype.isPrototypeOf(instance));

問題1.包含引用類型值的原型。

問題2.不能像超類型的構造方法傳參。

2.借用構造函數(constructor stealing)(偽造對象或經典繼承)

(1)無參:

function SuperType(){	this.colors = ['red','blue','green'];}function SubType(){	SuperType.call(this);}var instance1 = new SubType();instance1.colors.push("black");alert(instance1.colors);	var instance2 = new SubType();var instance3 = new SuperType();alert(instance3.colors);(2)傳參:

function SuperType(name){	this.name = name;}function SubType(){	SuperType.call(this,"zhangsan");	this.age = 20;}var instance1 = new SubType();alert(instance1.name);  //zhangsanalert(instance1.age);  //20var instance2 = new SuperType();alert(instance2.name);  //undefinedalert(instance2.age);  //undefined

問題:方法在構造函數中定義,無法復用。

3.組合繼承(combination inheritance)(偽經典繼承)

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;}//繼承方法SubType.prototype = new SuperType();SubType.prototype.sayAge = function(){	alert(this.age);}var instance1 = new SubType("zhangsan",20);instance1.colors.push("black");alert(instance1.colors);    // red,blue,green,blackinstance1.sayName();  //zhangsaninstance1.sayAge();   // 20var instance2 = new SubType("lisi",30);alert(instance2.colors);  // red,blue,greeninstance2.sayName();  //lisiinstance2.sayAge();  // 304. 原型式繼承(Prototypal inheritance)
function object(o){	function F(){}	F.prototype = o;	return new F();}var person = {	name:"zhangsan",	friends :["guojing","huangrong"]};var anotherPerson = object(person);anotherPerson.name = "lisi";anotherPerson.friends.push("zhoubotong");var yetAnotherPerson = object(person);yetAnotherPerson.name = "wangwu";yetAnotherPerson.friends.push("yidengdashi");alert(person.friends);  //  guojing,huangrong,zhoubotong,yidengdashi5.寄生式繼承(parasitic)
function object(o){	function F(){}	F.prototype = o;	return new F();}function createAnother(original){	var clone = object(original);	clone.sayHi = function(){		alert("hello");	};	return clone;}var person = {	name:"zhangsan",	friends :["guojing","huangrong"]};var anotherPerson = createAnother(person);//anotherPerson.sayHi();  //helloanotherPerson.name = "lisi";anotherPerson.friends.push("zhoubotong");var yetAnotherPerson = object(person);yetAnotherPerson.name = "wangwu";yetAnotherPerson.friends.push("yidengdashi");alert(person.friends);  //  guojing,huangrong,zhoubotong,yidengdashialert(anotherPerson.friends);  //  guojing,huangrong,zhoubotong,yidengdashialert(yetAnotherPerson.friends);  //  guojing,huangrong,zhoubotong,yidengdashi6.寄生組合式繼承
function object(o){	function F(){}	F.prototype = o;	return new F();}function inheritPrototype(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;}inheritPrototype(SubType,SuperType);SubType.prototype.sayAge = function(){	alert(this.age);}var instance = new SubType("zhangsan",30);alert(instance.name);  //zhangsaninstance.sayName();  //zhangsan	instance.sayAge();   // 30


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 昭通市| 黄石市| 枣阳市| 威海市| 卢湾区| 康平县| 循化| 吉木萨尔县| 三亚市| 庐江县| 华阴市| 高安市| 江津市| 甘孜县| 容城县| 和顺县| 平乡县| 呼和浩特市| 红河县| 房产| 临沧市| 峨眉山市| 古交市| 佛坪县| 涪陵区| 新乐市| 新乡县| 安达市| 济源市| 那曲县| 贵阳市| 兴宁市| 高阳县| 中卫市| 黄平县| 清水河县| 溧水县| 东乡县| 河南省| 西乡县|