最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript中的面向?qū)ο?/h1>
 更新時(shí)間:2017年03月30日 09:26:44   作者:閣主乄  
本文主要介紹了javascript中面向?qū)ο蟮南嚓P(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧

相信大家對(duì)javascript中的面向?qū)ο髮懛ǘ疾荒吧?,那還記得有幾種創(chuàng)建對(duì)象的寫法嗎?相信大家除了自己常寫的都有點(diǎn)模糊了,那接下來就由我來幫大家回憶回憶吧!

1. 構(gòu)造函數(shù)模式

通過創(chuàng)建自定義的構(gòu)造函數(shù),來定義自定義對(duì)象類型的屬性和方法。

function cons(name,age){
 this.name = name;
 this.age = age;
 this.getMes = function(){
  console.log(`my name is ${this.name},this year ${this.age}`);
 }
}
var mesge = new cons('will',21);
mesge.getMes();

2. 工廠模式

該模式抽象了創(chuàng)建具體對(duì)象的過程,用函數(shù)來封裝以特定接口創(chuàng)建對(duì)象的細(xì)節(jié)

function cons(name,age){
 var obj = new Object();
 obj.name = name;
 obj.age = age;
 obj.getMes = function(){
  console.log(`my name is ${this.name},this year ${this.age}`);
 }
 return obj;
}
var mesge = cons('will',21);
mesge.getMes();

3. 字面量模式

字面量可以用來創(chuàng)建單個(gè)對(duì)象,但如果要?jiǎng)?chuàng)建多個(gè)對(duì)象,會(huì)產(chǎn)生大量的重復(fù)代碼

var cons = {
 name: 'will',
 age : 21,
 getMes: function(){
  console.log(`my name is ${this.name},this year ${this.age}`);
 }
}
cons.getMes();

4. 原型模式

使用原型對(duì)象,可以讓所有實(shí)例共享它的屬性和方法

function cons(){
 cons.prototype.name = "will";
 cons.prototype.age = 21;
 cons.prototype.getMes = function(){
  console.log(`my name is ${this.name},this year ${this.age}`);
 }
}

var mesge = new cons();
mesge.getMes();

var mesge1 = new cons();
mesge1.getMes();
console.log(mesge.sayName == mesge1.sayName);//true

5. 組合模式

最常見的方式。構(gòu)造函數(shù)模式用于定義實(shí)例屬性,而原型模式用于定義方法和共享的屬性,這種組合模式還支持向構(gòu)造函數(shù)傳遞參數(shù)。實(shí)例對(duì)象都有自己的一份實(shí)例屬性的副本,同時(shí)又共享對(duì)方法的引用,最大限度地節(jié)省了內(nèi)存。該模式是目前使用最廣泛、認(rèn)同度最高的一種創(chuàng)建自定義對(duì)象的模式

function cons(name,age){
 this.name = name;
 this.age = age;
 this.friends = ["arr","all"];
}
cons.prototype = {
 getMes : function(){
  console.log(`my name is ${this.name},this year ${this.age}`);
 }
}
var mesge = new cons("will",21);
var mesge1 = new cons("jalo",21);
console.log(mesge.friends);
mesge.friends.push('wc'); //還可以操作數(shù)組哈O(∩_∩)O!
console.log(mesge.friends);
console.log(mesge1.friends);
mesge.getMes();
mesge1.getMes();
console.log(mesge.friends === mesge1.friends);
console.log(mesge.sayName === mesge1.sayName);

最后在告訴你個(gè)秘密,ES6引入了類(Class),讓對(duì)象的創(chuàng)建、繼承更加直觀了

// 定義類

class Cons{
 constructor(name,age){
  this.name = name;
  this.age = age;
 }
 getMes(){
  console.log(`hello ${this.name} !`);
 }
}
let mesge = new Cons('啦啦啦~',21);
mesge.getMes();

在上面的代碼片段里,先是定義了一個(gè)Cons類,里面還有一個(gè)constructor函數(shù),這就是構(gòu)造函數(shù)。而this關(guān)鍵字則代表實(shí)例對(duì)象。

而繼承可以通過extends關(guān)鍵字實(shí)現(xiàn)。

class Ctrn extends Cons{
 constructor(name,anu){

  super(name); //等同于super.constructor(x)
  this.anu = anu;
 }
 ingo(){
  console.log(`my name is ${this.name},this year ${this.anu}`);
 }
}
let ster = new Ctrn('will',21);
ster.ingo();
ster.getMes();

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論

万州区| 东平县| 巨野县| 南阳市| 新宾| 惠来县| 沽源县| 临澧县| 陇西县| 疏勒县| 额尔古纳市| 崇明县| 清水县| 东城区| 东乌| 堆龙德庆县| 土默特右旗| 荆门市| 婺源县| 年辖:市辖区| 同德县| 格尔木市| 渭源县| 竹溪县| 壶关县| 云林县| 宾阳县| 嘉黎县| 区。| 海安县| 黄龙县| 晋城| 剑河县| 万州区| 富裕县| 宜宾县| 惠州市| 辉南县| 遂溪县| 辽宁省| 江陵县|