Skip to Main Content
JavaScript 深入精要
book

JavaScript 深入精要

by Cody Lindley
December 2013
Intermediate to advanced content levelIntermediate to advanced
160 pages
2h 41m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 深入精要
this 的概念,以及它如何參考物件
JavaScript 建立函式之後,會建立一個稱為
this
的關鍵字(幕後進行),它會連結到
函式所屬的物件。換句話說,你可以在
this
所處的函式範圍內使用它,但它會參考到該
函式所屬的物件。
我們來回顧一下第一章的
cody
物件:
Live Codeh ttp://jsddle. net/javascript enlightenment/ yMXec/
<!DOCTYPE html><html lang="en"><body><script>
var cody = {
living : true,
age :23,
gender : 'male',
getGender : function() {return cody.gender;}
};
console.log(cody.getGender()); // log 'male'
</script></body></html>
注意我們如何在
cody
物件本身的
getGender
函式裡面,使用句點來存取
gender
屬性(例
cody.gender
)。我們也可以改用
this
來存取
cody
物件,因為
this
會指向
cody
物件。
Live Codeh ttp://jsddle. net/javascript enlightenment/ dDvPa/
this 關鍵字
第六章
70
   
第六章
<!DOCTYPE html><html lang="en"><body> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript 設計模式

JavaScript 設計模式

Stoyan Stefanov
HTML5 錦囊妙計

HTML5 錦囊妙計

Christopher Schmitt, Kyle Simpson
jQuery Mobile建置與執行

jQuery Mobile建置與執行

Maximiliano Firtman

Publisher Resources

ISBN: 9789863470120