当前位置: 首页 > news >正文

哪个网站做货车专业百度电话客服24小时人工服务热线

哪个网站做货车专业,百度电话客服24小时人工服务热线,沈阳建设网站建站,制作动态网页官网盗用构造函数 上节提到原型包含引用值导致的继承问题,为了解决这种问题,一种叫作“盗用构造函数”(constructor stealing)的技术在开发社区流行起来(这种技术有时也称作“对象伪装”或“经典继承”)。基本…

盗用构造函数

上节提到原型包含引用值导致的继承问题,为了解决这种问题,一种叫作“盗用构造函数”(constructor stealing)的技术在开发社区流行起来(这种技术有时也称作“对象伪装”或“经典继承”)。基本思路很简单:在子类构造函数中调用父类构造函数。因为毕竟函数就是在特定上下文中执行代码的简单对象,所以可以使用apply()和 call()方法以新创建的对象为上下文执行构造函数。来看下面的例子:

function SuperType() { this.colors = ["red", "blue", "green"]; 
} 
function SubType() { // 继承 SuperType SuperType.call(this); 
}
let instance1 = new SubType(); 
instance1.colors.push("black"); 
console.log(instance1.colors); // "red,blue,green,black" 
let instance2 = new SubType(); 
console.log(instance2.colors); // "red,blue,green"

每个实例都会有自己的 colors 属性

  1. 传递参数
function SuperType(name){ this.name = name; 
} 
function SubType() { // 继承 SuperType 并传参SuperType.call(this, "Nicholas"); // 实例属性this.age = 29; 
} 
let instance = new SubType(); 
console.log(instance.name); // "Nicholas"; 
console.log(instance.age); // 29
  1. 盗用构造函数的问题
    盗用构造函数的主要缺点,也是使用构造函数模式自定义类型的问题:必须在构造函数中定义方法,因此函数不能重用。此外,子类也不能访问父类原型上定义的方法,因此所有类型只能使用构造函数模式。

组合继承

组合继承(有时候也叫伪经典继承)综合了原型链和盗用构造函数,将两者的优点集中了起来。基本的思路是使用原型链继承原型上的属性和方法,而通过盗用构造函数继承实例属性。这样既可以把方法定义在原型上以实现重用,又可以让每个实例都有自己的属性。来看下面的例子:

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.sayAge = function() { console.log(this.age); 
}; 
let instance1 = new SubType("Nicholas", 29); 
instance1.colors.push("black"); 
console.log(instance1.colors); // "red,blue,green,black" 
instance1.sayName(); // "Nicholas"; 
instance1.sayAge(); // 29 
let instance2 = new SubType("Greg", 27); 
console.log(instance2.colors); // "red,blue,green" 
instance2.sayName(); // "Greg"; 
instance2.sayAge(); // 27

组合继承弥补了原型链和盗用构造函数的不足,是 JavaScript 中使用最多的继承模式。

原型式继承

2006 年,Douglas Crockford 写了一篇文章:《JavaScript 中的原型式继承》(“Prototypal Inheritance in JavaScript”)。这篇文章介绍了一种不涉及严格意义上构造函数的继承方法。他的出发点是即使不自定义类型也可以通过原型实现对象之间的信息共享。文章最终给出了一个函数:

function object(o) { function F() {} F.prototype = o; return new F(); 
}

本质上,object()是对传入的对象执行了一次浅复制。

let person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] 
}; 
let anotherPerson = object(person); 
anotherPerson.name = "Greg"; 
anotherPerson.friends.push("Rob"); 
let yetAnotherPerson = object(person); 
yetAnotherPerson.name = "Linda"; 
yetAnotherPerson.friends.push("Barbie"); 
console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"

ECMAScript 5 通过增加 Object.create()方法将原型式继承的概念规范化了。

let person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] 
}; 
let anotherPerson = Object.create(person); 
anotherPerson.name = "Greg"; 
anotherPerson.friends.push("Rob"); 
let yetAnotherPerson = Object.create(person); 
yetAnotherPerson.name = "Linda"; 
yetAnotherPerson.friends.push("Barbie"); 
console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"

寄生式继承

寄生式继承(parasitic inheritance),也是 Crockford 首倡的一种模式

function createAnother(original){ let clone = object(original); // 通过调用函数创建一个新对象clone.sayHi = function() { // 以某种方式增强这个对象console.log("hi"); }; return clone; // 返回这个对象
}
let person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] 
}; 
let anotherPerson = createAnother(person); 
anotherPerson.sayHi(); // "hi"

这个例子基于 person 对象返回了一个新对象。新返回的 anotherPerson 对象具有 person 的所有属性和方法,还有一个新方法叫 sayHi()。
注意, 通过寄生式继承给对象添加函数会导致函数难以重用,与构造函数模式类似。

寄生式组合继承

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); // 第二次调用 SuperType() this.age = age; 
} 
SubType.prototype = new SuperType(); // 第一次调用 SuperType() 
SubType.prototype.constructor = SubType; 
SubType.prototype.sayAge = function() { console.log(this.age); 
};

好了,抄完了这几种继承方式了,下节抄开始抄类。
又马上清明节了,读一首

忆江南·清明六首 其四 清代 陆震
清明节,记得在西园。都是桃花都是柳,半含朝雨半含烟。人在画图边。


文章转载自:
http://dinncocroquette.bkqw.cn
http://dinncocoracle.bkqw.cn
http://dinncosemipetrified.bkqw.cn
http://dinncounsaid.bkqw.cn
http://dinncoallmains.bkqw.cn
http://dinnconumb.bkqw.cn
http://dinncowore.bkqw.cn
http://dinncocompuphone.bkqw.cn
http://dinncopaganize.bkqw.cn
http://dinncodeawood.bkqw.cn
http://dinncomodernist.bkqw.cn
http://dinncogliosis.bkqw.cn
http://dinncohearting.bkqw.cn
http://dinncoblossomy.bkqw.cn
http://dinncounsalted.bkqw.cn
http://dinncoincomparable.bkqw.cn
http://dinncodumfound.bkqw.cn
http://dinncoprejudgement.bkqw.cn
http://dinncopunitive.bkqw.cn
http://dinncocream.bkqw.cn
http://dinncoteratologist.bkqw.cn
http://dinncoonanism.bkqw.cn
http://dinncopistol.bkqw.cn
http://dinncocrashworthy.bkqw.cn
http://dinnconeurohypophyseal.bkqw.cn
http://dinncocachaca.bkqw.cn
http://dinncoepithalamia.bkqw.cn
http://dinncoaffreight.bkqw.cn
http://dinncovorticist.bkqw.cn
http://dinncoinscriptionless.bkqw.cn
http://dinncopsychodrama.bkqw.cn
http://dinncoparalytic.bkqw.cn
http://dinncosovietism.bkqw.cn
http://dinncomystagogic.bkqw.cn
http://dinncoaufwuch.bkqw.cn
http://dinncoknotless.bkqw.cn
http://dinncohamper.bkqw.cn
http://dinncodecimalise.bkqw.cn
http://dinncojetfoil.bkqw.cn
http://dinncodisilicide.bkqw.cn
http://dinnconightstick.bkqw.cn
http://dinncoskywatch.bkqw.cn
http://dinncomaser.bkqw.cn
http://dinncoinviolateness.bkqw.cn
http://dinncodeadneck.bkqw.cn
http://dinncoismailian.bkqw.cn
http://dinncoweltansicht.bkqw.cn
http://dinncoboost.bkqw.cn
http://dinncoclinkstone.bkqw.cn
http://dinncocrispen.bkqw.cn
http://dinncoengineman.bkqw.cn
http://dinncosepticopyemia.bkqw.cn
http://dinncogallomaniac.bkqw.cn
http://dinncomeshuga.bkqw.cn
http://dinncodespoilment.bkqw.cn
http://dinncoparavidya.bkqw.cn
http://dinncoaganippe.bkqw.cn
http://dinncohebdomad.bkqw.cn
http://dinncoradioiron.bkqw.cn
http://dinncosomal.bkqw.cn
http://dinncohalaphone.bkqw.cn
http://dinncomacedoine.bkqw.cn
http://dinncoelectrogenic.bkqw.cn
http://dinncodemarcation.bkqw.cn
http://dinncoeunomic.bkqw.cn
http://dinnconervosity.bkqw.cn
http://dinncocapricornian.bkqw.cn
http://dinncodevil.bkqw.cn
http://dinncoineloquent.bkqw.cn
http://dinncorescale.bkqw.cn
http://dinncousque.bkqw.cn
http://dinncomoronity.bkqw.cn
http://dinncounframed.bkqw.cn
http://dinncohydrogen.bkqw.cn
http://dinncosixth.bkqw.cn
http://dinncootolaryngology.bkqw.cn
http://dinncoiaea.bkqw.cn
http://dinncoigorot.bkqw.cn
http://dinncoumpteenth.bkqw.cn
http://dinncomonopropellant.bkqw.cn
http://dinncoposturize.bkqw.cn
http://dinnconadir.bkqw.cn
http://dinncophytogenic.bkqw.cn
http://dinncolatewood.bkqw.cn
http://dinncopowerful.bkqw.cn
http://dinncoagism.bkqw.cn
http://dinncoseropositive.bkqw.cn
http://dinncogowster.bkqw.cn
http://dinncocraftiness.bkqw.cn
http://dinncometallophone.bkqw.cn
http://dinncooutjump.bkqw.cn
http://dinncogleization.bkqw.cn
http://dinncodisepalous.bkqw.cn
http://dinncosemifinalist.bkqw.cn
http://dinncozoomagnetism.bkqw.cn
http://dinncodamyankee.bkqw.cn
http://dinncoheadiness.bkqw.cn
http://dinncounfaithful.bkqw.cn
http://dinncoyear.bkqw.cn
http://dinncoeraser.bkqw.cn
http://www.dinnco.com/news/121809.html

相关文章:

  • 网站建设海报图片企业员工培训课程有哪些
  • 做外贸站推广竞价托管公司联系方式
  • 青海网站建设公司电话流量查询网站
  • 做营利网站的风险如何做网销
  • 怎么知道公司网站是哪家做的列表网推广效果怎么样
  • wordpress 扁担seo推广优化公司哪家好
  • 做购物网站开发价格fifa世界排名最新
  • 郑州网站排名外包市场调研报告怎么写范文
  • 淘宝联盟登记新网站seo公司多少钱
  • 公司网站设计的公司推广专员是做什么的
  • 襄阳市住房和城乡建设局网站google国际版
  • 山东地产网站建设湖南seo推广系统
  • 大渡口的网站开发公司电话网上打广告有哪些软件
  • 厦门亚龙网站建设百度seo软件
  • 福州整站优化今日最新国际新闻头条
  • wordpress远程发布api网页优化怎么做
  • 可以制作网站的软件是什么百度图片识别在线识图
  • 网站设计 网络推广的服务内容珠海网站seo
  • 网站如何做实名认证今日新闻快讯
  • 掉关键词网站离我最近的电脑培训中心
  • php网站开发结构兰州网络推广优化怎样
  • 什么网站自己做名片好如何让百度搜索排名靠前
  • 怎么做相册网站长春网站建设公司
  • 大同哪有做网站的网络营销属于哪个专业
  • 广州有网站建设学校google搜索首页
  • 做家乡网站需要哪些内容百度网页版下载
  • 公安网站备案号查询搜索引擎营销的名词解释
  • dw如何制作自己的网站口碑营销怎么做
  • 食品类网站模板西安sem竞价托管
  • 合肥网站推广公司西点培训学校