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

首頁 > 編程 > JavaScript > 正文

js老生常談之this,constructor ,prototype全面解析

2019-11-20 10:17:29
字體:
來源:轉載
供稿:網友

前言

javascript中的this,constructor ,prototype,都是老生常談的問題,深入理解他們的含義至關重要。在這里,我們再來復習一下吧,溫故而知新!

this

this表示當前對象,如果在全局作用范圍內使用this,則指代當前頁面對象window; 如果在函數中使用this,則this指代什么是根據運行時此函數在什么對象上被調用。 我們還可以使用apply和call兩個全局方法來改變函數中this的具體指向。

先看一個在全局作用范圍內使用this的例子:

console.log(this === window); // trueconsole.log(window.alert === this.alert); // trueconsole.log(this.parseInt("021", 10)); // 10

函數中的this是在運行時決定的,而不是函數定義時,如下:

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

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

// 定義一個全局函數    function foo() {      console.log(this.fruit);    }    // 定義一個全局變量    var fruit = "apple";    // 自定義一個對象    var pack = {      fruit: "orange"    };    // 等價于window.foo();    foo.apply(window); // "apple"    // 此時foo中的this === pack    foo.apply(pack);  // "orange"

注:apply和call兩個函數的作用相同,唯一的區別是兩個函數的參數定義不同。

因為在JavaScript中函數也是對象,所以我們可以看到如下有趣的例子:

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

prototype

prototype本質上還是一個JavaScript對象。

并且每個函數都有一個默認的prototype屬性。 如果這個函數被用在創建自定義對象的場景中,我們稱這個函數為構造函數。 比如下面一個簡單的場景:

// 構造函數    function Person(name) {      this.name = name;    }    // 定義Person的原型,原型中的屬性可以被自定義對象引用    Person.prototype = {      getName: function() {        return this.name;      }    }    var hao= new Person("haorooms");    console.log(hao.getName());  // "haorooms"

作為類比,我們考慮下JavaScript中的數據類型 - 字符串(String)、數字(Number)、數組(Array)、對象(Object)、日期(Date)等。

我們有理由相信,在JavaScript內部這些類型都是作為構造函數來實現的,比如:

// 定義數組的構造函數,作為JavaScript的一種預定義類型    function Array() {      // ...    }    // 初始化數組的實例    var arr1 = new Array(1, 56, 34, 12);    // 但是,我們更傾向于如下的語法定義:    var arr2 = [1, 56, 34, 12];

同時對數組操作的很多方法(比如concat、join、push)應該也是在prototype屬性中定義的。 實際上,JavaScript所有的固有數據類型都具有只讀的prototype屬性 (這是可以理解的:因為如果修改了這些類型的prototype屬性,則哪些預定義的方法就消失了), 但是我們可以向其中添加自己的擴展方法。

// 向JavaScript固有類型Array擴展一個獲取最小值的方法    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的實例上調用min方法    console.log([1, 56, 34, 12].min()); // 1

注意:這里有一個陷阱,向Array的原型中添加擴展方法后,當使用for-in循環數組時,這個擴展方法也會被循環出來。 下面的代碼說明這一點(假設已經向Array的原型中擴展了min方法):

var arr = [1, 56, 34, 12];    var total = 0;    for (var i in arr) {      total += parseInt(arr[i], 10);    }    console.log(total);  // NaN

解決方法也很簡單:

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始終指向創建當前對象的構造函數。比如下面例子:

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

但是當constructor遇到prototype時,有趣的事情就發生了。 我們知道每個函數都有一個默認的屬性prototype,而這個prototype的constructor默認指向這個函數。如下例所示:

function Person(name) {      this.name = name;    };    Person.prototype.getName = function() {      return this.name;    };    var p = new Person("haorooms");    console.log(p.constructor === Person); // true    console.log(Person.prototype.constructor === Person); // true    // 將上兩行代碼合并就得到如下結果    console.log(p.constructor.prototype.constructor === Person); // true

當時當我們重新定義函數的prototype時(注意:和上例的區別,這里不是修改而是覆蓋), constructor的行為就有點奇怪了,如下示例:

function Person(name) {      this.name = name;    };    Person.prototype = {      getName: function() {        return this.name;      }    };    var p = new Person("haorooms");    console.log(p.constructor === Person); // false    console.log(Person.prototype.constructor === Person); // false    console.log(p.constructor.prototype.constructor === Person); // false

為什么呢? 原來是因為覆蓋Person.prototype時,等價于進行如下代碼操作:

Person.prototype = new Object({      getName: function() {        return this.name;      }    });

而constructor始終指向創建自身的構造函數,所以此時Person.prototype.constructor === Object,即是:

function Person(name) {      this.name = name;    };    Person.prototype = {      getName: function() {        return this.name;      }    };    var p = new Person("haorooms");    console.log(p.constructor === Object); // true    console.log(Person.prototype.constructor === Object); // true    console.log(p.constructor.prototype.constructor === Object); // true

怎么修正這種問題呢?方法也很簡單,重新覆蓋Person.prototype.constructor即可:

function Person(name) {      this.name = name;    };    Person.prototype = {      getName: function() {        return this.name;      }    };    Person.prototype.constructor = Person;    var p = new Person("haorooms");    console.log(p.constructor === Person); // true    console.log(Person.prototype.constructor === Person); // true    console.log(p.constructor.prototype.constructor === Person); // true

也可以這么寫:

function Person(name) {      this.name = name;    };    Person.prototype = {     constructor:Person,//指定constructor      getName: function() {        return this.name;      }    };

以上這篇js老生常談之this,constructor ,prototype全面解析就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永宁县| 洪雅县| 安多县| 长白| 宁安市| 天门市| 农安县| 金湖县| 南澳县| 黑龙江省| 苍山县| 包头市| 阳东县| 子长县| 达拉特旗| 绥阳县| 拜城县| 土默特左旗| 金坛市| 新巴尔虎左旗| 白沙| 洪江市| 穆棱市| 沐川县| 依安县| 曲靖市| 塘沽区| 苍溪县| 区。| 湘西| 宝应县| 东明县| 日土县| 聂拉木县| 邮箱| 清远市| 珲春市| 敖汉旗| 山西省| 大渡口区| 庆云县|