四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

JavaScript 繼承詳解(二)

this

this表示當(dāng)前對(duì)象,如果在全局作用范圍內(nèi)使用this,則指代當(dāng)前頁(yè)面對(duì)象window; 如果在函數(shù)中使用this,則this指代什么是根據(jù)運(yùn)行時(shí)此函數(shù)在什么對(duì)象上被調(diào)用。 我們還可以使用apply和call兩個(gè)全局方法來改變函數(shù)中this的具體指向。

先看一個(gè)在全局作用范圍內(nèi)使用this的例子:

  <script type="text/Javascript">   console.log(this === window); // true   console.log(window.alert === this.alert); // true   console.log(this.parseInt("021", 10)); // 10  </script>  

 

函數(shù)中的this是在運(yùn)行時(shí)決定的,而不是函數(shù)定義時(shí),如下:

  // 定義一個(gè)全局函數(shù)  function foo() {   console.log(this.fruit);  }  // 定義一個(gè)全局變量,等價(jià)于window.fruit = "apple";  var fruit = "apple";  // 此時(shí)函數(shù)foo中this指向window對(duì)象  // 這種調(diào)用方式和window.foo();是完全等價(jià)的  foo(); // "apple"  // 自定義一個(gè)對(duì)象,并將此對(duì)象的屬性foo指向全局函數(shù)foo  var pack = {   fruit: "orange",   foo: foo  };  // 此時(shí)函數(shù)foo中this指向window.pack對(duì)象  pack.foo(); // "orange"  

 

全局函數(shù)apply和call可以用來改變函數(shù)中this的指向,如下:

  // 定義一個(gè)全局函數(shù)  function foo() {   console.log(this.fruit);  }    // 定義一個(gè)全局變量  var fruit = "apple";  // 自定義一個(gè)對(duì)象  var pack = {   fruit: "orange"  };    // 等價(jià)于window.foo();  foo.apply(window); // "apple"  // 此時(shí)foo中的this === pack  foo.apply(pack); // "orange"  
注:apply和call兩個(gè)函數(shù)的作用相同,唯一的區(qū)別是兩個(gè)函數(shù)的參數(shù)定義不同。

 

因?yàn)樵?a href=/itjie/Javajishu/ target=_blank class=infotextkey>JavaScript中函數(shù)也是對(duì)象,所以我們可以看到如下有趣的例子:

  // 定義一個(gè)全局函數(shù)  function foo() {   if (this === window) {    console.log("this is window.");   }  }    // 函數(shù)foo也是對(duì)象,所以可以定義foo的屬性boo為一個(gè)函數(shù)  foo.boo = function() {   if (this === foo) {    console.log("this is foo.");   } else if (this === window) {    console.log("this is window.");   }  };  // 等價(jià)于window.foo();  foo(); // this is window.    // 可以看到函數(shù)中this的指向調(diào)用函數(shù)的對(duì)象  foo.boo(); // this is foo.    // 使用apply改變函數(shù)中this的指向  foo.boo.apply(window); // this is window.  

 

prototype

我們已經(jīng)在第一章中使用prototype模擬類和繼承的實(shí)現(xiàn)。 prototype本質(zhì)上還是一個(gè)JavaScript對(duì)象。 并且每個(gè)函數(shù)都有一個(gè)默認(rèn)的prototype屬性。
如果這個(gè)函數(shù)被用在創(chuàng)建自定義對(duì)象的場(chǎng)景中,我們稱這個(gè)函數(shù)為構(gòu)造函數(shù)。 比如下面一個(gè)簡(jiǎn)單的場(chǎng)景:

  // 構(gòu)造函數(shù)  function Person(name) {   this.name = name;  }  // 定義Person的原型,原型中的屬性可以被自定義對(duì)象引用  Person.prototype = {   getName: function() {    return this.name;   }  }  var zhang = new Person("ZhangSan");  console.log(zhang.getName()); // "ZhangSan"  
作為類比,我們考慮下JavaScript中的數(shù)據(jù)類型 - 字符串(String)、數(shù)字(Number)、數(shù)組(Array)、對(duì)象(Object)、日期(Date)等。 我們有理由相信,在JavaScript內(nèi)部這些類型都是作為構(gòu)造函數(shù)來實(shí)現(xiàn)的,比如:
  // 定義數(shù)組的構(gòu)造函數(shù),作為JavaScript的一種預(yù)定義類型  function Array() {   // ...  }    // 初始化數(shù)組的實(shí)例  var arr1 = new Array(1, 56, 34, 12);  // 但是,我們更傾向于如下的語法定義:  var arr2 = [1, 56, 34, 12];  
同時(shí)對(duì)數(shù)組操作的很多方法(比如concat、join、push)應(yīng)該也是在prototype屬性中定義的。
實(shí)際上,JavaScript所有的固有數(shù)據(jù)類型都具有只讀的prototype屬性(這是可以理解的:因?yàn)槿绻薷牧诉@些類型的prototype屬性,則哪些預(yù)定義的方法就消失了), 但是我們可以向其中添加自己的擴(kuò)展方法。
  // 向JavaScript固有類型Array擴(kuò)展一個(gè)獲取最小值的方法  Array.prototype.min = function() {   var min = this[0];   for (var i = 1; i < this.length; i++) {    if (this[i] < min) {     min = this[i];    }   }   return min;  };    // 在任意Array的實(shí)例上調(diào)用min方法  console.log([1, 56, 34, 12].min()); // 1  

注意:這里有一個(gè)陷阱,向Array的原型中添加擴(kuò)展方法后,當(dāng)使用for-in循環(huán)數(shù)組時(shí),這個(gè)擴(kuò)展方法也會(huì)被循環(huán)出來。
下面的代碼說明這一點(diǎn)(假設(shè)已經(jīng)向Array的原型中擴(kuò)展了min方法):
  var arr = [1, 56, 34, 12];  var total = 0;  for (var i in arr) {   total += parseInt(arr[i], 10);  }  console.log(total); // NaN    
解決方法也很簡(jiǎn)單:
  var arr = [1, 56, 34, 12];  var total = 0;  for (var i in arr) {   if (arr.hasOwnProperty(i)) {    total += parseInt(arr[i], 10);   }  }  console.log(total); // 103  

 

constructor

constructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。比如下面例子:

  // 等價(jià)于 var foo = new Array(1, 56, 34, 12);  var arr = [1, 56, 34, 12];  console.log(arr.constructor === Array); // true  // 等價(jià)于 var foo = new Function();  var Foo = function() { };  console.log(Foo.constructor === Function); // true  // 由構(gòu)造函數(shù)實(shí)例化一個(gè)obj對(duì)象  var obj = new Foo();  console.log(obj.constructor === Foo); // true    // 將上面兩段代碼合起來,就得到下面的結(jié)論  console.log(obj.constructor.constructor === Function); // true  

 

但是當(dāng)constructor遇到prototype時(shí),有趣的事情就發(fā)生了。
我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(shù)。如下例所示:

  function Person(name) {   this.name = name;  };  Person.prototype.getName = function() {   return this.name;  };  var p = new Person("ZhangSan");    console.log(p.constructor === Person); // true  console.log(Person.prototype.constructor === Person); // true  // 將上兩行代碼合并就得到如下結(jié)果  console.log(p.constructor.prototype.constructor === Person); // true  
當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋), constructor的行為就有點(diǎn)奇怪了,如下示例:
  function Person(name) {   this.name = name;  };  Person.prototype = {   getName: function() {    return this.name;   }  };  var p = new Person("ZhangSan");  console.log(p.constructor === Person); // false  console.log(Person.prototype.constructor === Person); // false  console.log(p.constructor.prototype.constructor === Person); // false  
為什么呢?
原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:
  Person.prototype = new Object({   getName: function() {    return this.name;   }  });  
而constructor始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(shí)Person.prototype.constructor === Object,即是:
  function Person(name) {   this.name = name;  };  Person.prototype = {   getName: function() {    return this.name;   }  };  var p = new Person("ZhangSan");  console.log(p.constructor === Object); // true  console.log(Person.prototype.constructor === Object); // true  console.log(p.constructor.prototype.constructor === Object); // true  
怎么修正這種問題呢?方法也很簡(jiǎn)單,重新覆蓋Person.prototype.constructor即可:
  function Person(name) {   this.name = name;  };  Person.prototype = new Object({   getName: function() {    return this.name;   }  });  Person.prototype.constructor = Person;  var p = new Person("ZhangSan");  console.log(p.constructor === Person); // true  console.log(Person.prototype.constructor === Person); // true  console.log(p.constructor.prototype.constructor === Person); // true  

 


下一章我們將會(huì)對(duì)第一章提到的Person-Employee類和繼承的實(shí)現(xiàn)進(jìn)行完善。

JavaScript技術(shù)JavaScript 繼承詳解(二),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 《爱的温暖》电影在线观看| 情侣网站视频| 黑暗欲望| 李慧珍演的电影有哪些| 王韧| 天下无贼果宝特攻| 山楂树之恋35集免费观看电视剧| 三人越谷文言文翻译| 莫斯科保卫战电影| 生产时直拍宫口| 柳堡的故事演员表| 寻梦环游记英文| 西部往事 电影| 眼皮下垂手术费用多少钱 | 恶魔之吻1993完整版在线观看| 菊次郎的夏天钢琴谱简谱| 冷暖人间第一部| 女生下体长什么样| 翡翠台高清直播| 溜冰圆舞曲音乐教案| cctv6电影节目表| 有档期是有空还是没空| 电视剧《流金岁月》演员表| 用药错误应急预案演练脚本| 常蓝天| 网络谜踪2 电影| 协议过户什么意思| 杨采钰电影| 诈欺游戏电影| 一闪一闪亮晶晶钢琴谱简谱 | 邓佳佳| 创新点| 电视剧火流星演员表| 邓佳佳| 天鹅套索 电视剧| 潘馨| 女生打屁股视频| 欧美视频亚洲视频| 杨贵妃黄色片| 侯怡君| 我金子般的孩子|