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 深入精要
54
   
第四章
<!DOCTYPE html><html lang="en"><body><script>
var add = function() {
return arguments[0] + arguments[1];
};
console.log(add(4, 4)); // 回傳 8
</script></body></html>
this
關鍵字,如果被傳到任何函式,都會參考那個函式所屬的物件。如你預期,以屬性
的形式(即,方法)處於物件之中的函式可以用它來取得「父」 物件的參考。當某個函
式被定義為全域範圍時,
this
的值是全域物件。請重新檢視這段程式,並確定你已經瞭
this
究竟回傳什麼。
Live Codehttp://jsddle. net/javascript enlightenment/ WFzW3/
<!DOCTYPE html><html lang="en"><body><script>
var myObject1 = {
name: 'myObject1',
myMethod: function(){console.log(this);}
};
myObject1.myMethod(); // log 'myObject1'
var myObject2 = function(){console.log(this);};
myObject2(); // log window
</script></body></html>
arguments.callee 屬性
ar ...
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