javascript 有關this的理解

來源:互聯網
上載者:User

首先先看一段代碼:

 1 <script type = "text/javascript"> 2     function JSClass(){ 3         this.m_Text = 'division element'; 4         this.m_Element = document.createElement('div'); 5         this.m_Element.innerHTML = this.m_Text; 6              7         this.m_Element.addEventListener('click', this.ToString); 8     } 9    10     JSClass.prototype.Render = function(){11          document.body.appendChild(this.m_Element);12     }     13 14     JSClass.prototype.ToString = function(){15         alert(this.m_Text);16     };17   18 19     var jc = new JSClass();20     jc.Render(); 21     jc.ToString();22 </script>

運行結果:在頁面上建立出div,文本為division element,然後alert出division element,當我點擊div時將輸出"undefined"

自己的理解:“在頁面上建立出div,文本為division element,然後alert出division element”是因為此時this指的是JSClass的執行個體,而當我們點擊div時,此時this指向的div本身,沒有

m_Text屬性。

因此在編寫代碼過程中,可以使用一個變數來指向它,就不會因為環境的變化,發生變化了,自己理解,呵呵

下面是看別人的部落格總結:(感覺特別好)

 this的值取決於function被呼叫者式:

1、如果一個function是一個對象的屬性,該function被調用的時候,this的值是這個對象,如果function抵用的運算式包含句點或是[],this的值是他們前面的對象,如myObj.func 和myObj["func"] 中,func 被調用時的 this 是myObj。 

var person = {};      person.name = "shenmajs";      person.sayName = function(){      alert(this.name);};person.sayName();//彈出shenmajs,是person調用了sayName,this指向person

2、如果一個 function 不是作為一個對象的屬性,那麼該 function 被調用的時候,this 的值是全域對象。當一個 function 中包含內部 function 的時候,如果不理解 this 的正確含義,很容易造成錯誤。這是由於內部 function 的 this 值與它外部的 function 的 this 值是不一樣的。解決辦法是將外部 function 的 this 值儲存在一個變數中,在內部 function 中使用它來尋找變數。

1 var name = "window";2 function alertName(){3     alert(this.name);4 }5 alertName();6 //彈出window,直接調用alertName方法,相當於全域對象調用了此方法,全域變數name當作了全域對象的屬性

3、如果在一個 function 之前使用 new 的話,會建立一個新的對象,該 funtion 也會被調用,而 this 的值是新建立的那個對象。如function User(name) {this.name = name}; var user1 = new User("Alex"); 中,通過調用new User("Alex") ,會建立一個新的對象,以user1 來引用,User 這個 function 也會被調用,會在user1 這個對象中設定名為name 的屬性,其值是Alex 。

1 function Person (name){2     this.name = name;    //構造的過程中,this指向新的那個執行個體3     this.sayhello = function (){4         alert(this.name);5     }6 }7 var person = new Person("shenmajs"); 8 person.sayhello(); //彈出shenmajs ,因為sayhello方法是由person調用的
1 function alertName(){2     alert(this.name);3 }4 var name = "window";5 var person = {}; 6 person.name = "shenmajs"; 7 person.sayName = alertName; //sayName 與 alertName 都是指向那個方法執行個體的指標變數而已8 person.sayName(); //彈出shenmajs,是person調用了alertName,this指向person9 alertName(); //彈出window,直接調用alertName方法,相當於全域對象調用了此方法,全域變數name當作了全域對象的屬性
function Person (name,age){    this.age = age;    this.name = name;    this.sayHello = function(){        alert("hi," +this.name );    }; }var person = new Person("shenmajs",25);person.sayHello();//hi,shenmajsvar func = person.sayHello;// func 指向 function(){ alert("hi," +this.name );  它只是一個函數指標,與person.sayHello 指向一個函數var name = "window";func(); //hi,window,func直接調用 this指向全域對象

var name = "window";var arraytest = [function(){alert(this[2]);},function(){alert(this.name);},"shenmajs"];//函數也是一種類型,匿名函數在這裡當做數組的一個元素arraytest.name = "arraytest";var arrayfunc = [];function func(){  alert(this[1]);}arrayfunc[0] = func;arrayfunc[1] = 99;var functest1 = func;var functest2 = arraytest[1];arraytest[0]();//彈出shenmajs ,this指向arraytest,this[2] 等價於 arraytest[2]。我一開始也不理解為什麼會指向arraytest,只能死記了。arraytest[1]();//彈出arraytest,同樣,this指向 arraytest,this.name 相當於arraytest.name . 我給arraytest加了一個name屬性。這也是js動態語言的特性arrayfunc[0]();//彈出99 ,數組中的第一個元素指向了函數,這樣調用時this指向了 arrayfunc數組,同上面的道理 this[1] 相當於 arrayfunc[1]functest1();//彈出 undefined ,相當於直接調用 func,this指向全域對象 window[1]未定義,因此彈出undefinedfunctest2(); //彈出window ,functest2 = arraytest[1],變數functest2指向函數,這裡調用 即相當於直接調用,this指向全域變數。

 


 

length = 9;var foo = {  bar: function() {    return this.length;  },  length: 0};(function f(a, b) {  f.length = 1;   //f.length 表示函數形參個數,就是函數定義的括弧裡有幾個參數,此屬性不可寫,因此 f.length 永遠是2  .// http://ecma-international.org/ecma-262/5.1/#sec-15.3.3.2  alert(arguments[0]());//argumengts表示實參,即函數時間傳遞進來的參數,f立即執行時,傳進來三個參數 (..f..)(foo.bar,2,"str")//彈出3,this指向arguments,arguments.length 等於 3  alert(a());//彈出9, a指向 foo.bar所指向的函數,a()直接調用,this相當於window全域對象,this.length 等於 9  alert(f.length);  // 彈出2 ,此屬性不可寫  alert((foo.bar)());//彈出0,foo調用函數bar,this指向 foo。 需要說明的是 foo.bar() 和 (foo.bar)()的效果一樣。})(foo.bar,2,"str");

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.