麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > JavaScript > 正文

JavaScript面向?qū)ο笾^承

2019-11-06 06:37:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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));

問(wèn)題1.包含引用類型值的原型。

問(wèn)題2.不能像超類型的構(gòu)造方法傳參。

2.借用構(gòu)造函數(shù)(constructor stealing)(偽造對(duì)象或經(jīng)典繼承)

(1)無(wú)參:

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

問(wèn)題:方法在構(gòu)造函數(shù)中定義,無(wú)法復(fù)用。

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

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲一区二区三区视频 | 曰韩一二三区 | 免费国产精品视频 | 精品国产一区二区三区四区阿崩 | 国产韩国精品一区二区三区久久 | 一级黄色国产视频 | 一区二区久久久久草草 | 中文在线观看免费视频 | 在火车上摸两乳爽的大叫 | 欧美日韩一区,二区,三区,久久精品 | 免费看成人毛片 | 在线成人影视 | 高清av免费 | av视在线 | 国产精品99久久久久久久 | 欧美中文在线 | 国产一级做a爱片在线看免 2019天天干夜夜操 | 色阁五月 | 性看小视频| 黄视频网址 | 亚洲精品xxx | 91成人久久 | 91成人在线网站 | 永久免费黄色片 | 久久综合给合久久狠狠狠97色69 | lutube成人福利在线观看 | 精品一二三区视频 | 国产日本在线播放 | 欧美黄一级 | 亚洲成人福利在线观看 | 性欧美xxxx免费岛国不卡电影 | 成人午夜影院 | 免费国产人成网站 | 国产精品久久久久久久久久久久午夜 | 成人av一二三区 | 久久久三级免费电影 | 久久91久久久久麻豆精品 | 精品一区二区在线观看视频 | 蜜桃一本色道久久综合亚洲精品冫 | 日韩欧美色综合 | 精精国产xxxx视频在线播放7 |