徹底搞清javascript中this, constructor, prototype

說起這三個屬性,肯定有一些同學(xué)和我一樣,初學(xué)js時非常困惑,頭大,一臉的迷茫。今天就來給大家徹底解決這些擔心受怕的問題。

先看this

this定義:

this就是函數(shù)賴以執(zhí)行的對象。

分析這句話:

1. this是對象。

2. this依賴函數(shù)執(zhí)行的上下文環(huán)境。

3. this存在函數(shù)中。

直接看例子:

alert(this); //在全局環(huán)境調(diào)用this, this指向window,? 輸出[Object window]

function Person(){

? ? ?alert(this);

}

方式一:

Person(); // 全局環(huán)境用Person函數(shù), this指向window,? 輸出[Object window]

方式二:

var obj = new Person(); //把Person當做構(gòu)造函數(shù), 實例化一個對象

//此時this指向了obj, 不再指向window,? 輸出[Object object]

function Person(){

? ? ?alert(this.name); //此時無法判斷this的身份

}

Person(); //this在全局環(huán)境中被調(diào)用, this.name == window.name, 輸出了窗口的名字

var obj = new Person(); //this在obj環(huán)境下被調(diào)用, this.name == obj.name, 由于name沒被賦值, 所以輸出undefined

由此可以看出, 我們在閱讀代碼或者寫代碼時,看到某個函數(shù)中定義的this時, 還無法去判斷那個this身份,必須找到它依賴執(zhí)行的環(huán)境(對象)。

再回頭看看this的定義,大家就清楚自然了。

再看constructor和prototype

constructor和prototype的關(guān)系非常密切。

constructor是一個對象的屬性,這個屬性存在在此對象的prototype中, 指向此對象的構(gòu)造函數(shù)。

分析這句話:

1.constructor是一個對象屬性。

2.constructor在prototype中

3.constructor指向構(gòu)造函數(shù)

例子1:

function Person(name, age){

? ? ? this.name = name;

? ? ? this.age = age;

}

Person.prototype.getName = function(){

? ? ?alert(this.name);

}

Person.prototype.getAge = function(){

? ? ?alert(this.age);

}

var obj = new Person();

alert(obj.constructor == Person);// true

此種方式定義的prototype, constructor是隱藏的, 默認指向Person

例子2:

function Person(name, age){

? ? ?this.name = name;

? ? ?this.age = age;

}

Person.prototype = {

? ? ?getName: function(){

? ? ? ? ?alert(this.name);

?},

getAge: function(){

? ? ? ? ?alert(this.age);

? ?}

}

var obj = new Person();

alert(obj.constructor == Person);// false

為什么是false? 這種定義prototype, 是把prototype重寫了, 覆蓋了默認的constructor。

換句話說, 其實這種方式就是給屬性重新賦值了, 所以導(dǎo)致默認的constructor被覆蓋。

此時的obj.constructor將指向的是Object。

改寫一下上面的:

Person.prototype = {

? ? ? constructor: Person, //強制指向Person

? ? ? ?getName: function(){

? ? ? ? ? ? alert(this.name);

? ? ? },

? ? getAge: function(){

? ? ? ? alert(this.age);

? ? }

}

此時constructor就指向Person了。

prototype是一個函數(shù)屬性, 此屬性同時也是一個對象, 保存著對象實例所共有的屬性和方法。

分析這句話:

1.prototype是函數(shù)屬性, 只要是函數(shù), 就有prototype屬性. 而不管是構(gòu)造函數(shù)還是普通函數(shù).

2.prototype同時也是對象.

2.prototype放的是公共的東西, 包括屬性和方法.

例子1.

function Person(name, age){

? ? ? this.name = name;

? ? ? this.age = age;

}

//是函數(shù)就有prototype屬性, 這個屬性也是一個對象

Person.prototype = {

? ? ?getName: function(){ //所有對象實例都共享

? ? ?return this.name;

? },

? getAge: function(){//所有對象實例都共享

? ? ? return this.age;

? }

}

var obj = new Person('tom', 23);

obj.getName(); //'tom'

var obj2 = new Person('jack', 23);

obj2.getName(); //'jack'

obj.getName == obj2.getName; //true, 所有實例共享

Person.prototype.getName(); //當做普通函數(shù)屬性, 根據(jù)this定義, 此時this指向的是Person.prototype, 所以返回undefined

以上就是this, constructor, prototype的定義和他們之間的關(guān)系. 可能還有些粗糙, 歡迎大家補充.

綜合例子:

var Tinker = function(){

? ? ? ?this.elements = [];

};

Tinker.fn = Tinker.prototype = {

? ? ? ?constructor: Tinker,

? ? ? ?extend: function(obj){

? ? ? ? ? ? ?var p;

? ? ? ? ? ?for(p in obj){

? ? ? ? ? ? ? this.constructor.prototype[p] = obj[p];//此處若看明白了, 那么前面的就理解了

? ? ? ?}

? ? ?}

}

Tinker.fn.extend({

get: function(){

? ? ?var length = arguments.length,

? ? ? ? ? ? i = 0;

? ? ? ?for(; i < length; i++){

? ? ? ? ? this.elements.push(document.getElementById(arguments[i])); //此處若看明白了, 那么前面的就理解了

? ? ?}

? ? ? return this;//此處若看明白了, 那么前面的就理解了

},

each: function(fn){

? ? ? var i = 0,

? ? ? ? ?length = this.elements.length;

? ? ? ?for(; i < length; i++){

? ? ? ? ?fn.call(this.elements[i], i, this.elements[i]);

? ? ? }

? ? ? return this;//此處若看明白了, 那么前面的就理解了

? ?}

});

這個例子其實很簡單, 就是向一個對象原型添加方法.一個方法是get, 用于查找頁面id. 一個是each, 用于對找到的id元素執(zhí)行一個方法

//假設(shè)有id = 'data', id = 'message'

var obj = new Tinker();

obj.get('data', 'message').each(function(i, item){

? ? ?this.style.cssText = 'height:20px; background:#ff0000';

})

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容