JavaScript函数调用规则一 |
002 | |
003 | (1)全局函数调用: |
004 | function makeArray( arg1, arg2 ){ |
005 | return [ this , arg1 , arg2 ]; |
006 | } |
007 | 这是一个最常用的定义函数方式。相信学习JavaScript的人对它的调用并不陌生。 |
008 | 调用代码如下: |
009 | makeArray( 'one' , 'two' ); |
010 | // => [ window, 'one', 'two' ] |
011 | |
012 | 这种方式可以说是全局的函数调用。 |
013 | 为什么说是全局的函数? |
014 | 因为它是全局对象window 的一个方法, |
015 | 我们可以用如下方法验证: |
016 | alert( typeof window.methodThatDoesntExist ); |
017 | // => undefined |
018 | |
019 | alert( typeof window.makeArray); |
020 | // => function |
021 | |
022 | 所以我们之前调用 makeArray的方法是和下面调用的方法一样的 |
023 | window.makeArray( 'one' , 'two' ); |
024 | // => [ window, 'one', 'two' ] |
025 | |
026 | |
027 | |
028 | |
029 | JavaScript函数调用规则二 |
030 | |
031 | (1)对象方法调用: |
032 | //creating the object |
033 | var arrayMaker = { |
034 | someProperty: 'some value here' , |
035 | make: makeArray |
036 | }; |
037 | |
038 | arrayMaker.make( 'one' , 'two' ); // => [ arrayMaker, 'one', 'two' ] |
039 | //或者用下面的方法调用: |
040 | arrayMaker[ 'make' ]( 'one' , 'two' ); // => [ arrayMaker, 'one', 'two' ] |
041 | |
042 | |
043 | 看到这里跟刚才的区别了吧, this 的值变成了对象本身. |
044 | 你可能会质疑:为什么原始的函数定义并没有改变,而 this 却变化了呢? |
045 | |
046 | 非常好,有质疑是正确的。这里涉及到 函数在JavaScript中传递的方式, |
047 | 函数在JavaScript 里是一个标准的数据类型, |
048 | 确切的说是一个对象.你可以传递它们或者复制他们. |
049 | 就好像整个函数连带参数列表和函数体都被复制, |
050 | 且被分配给了 arrayMaker 里的属性 make,那就好像这样定义一个 arrayMaker : |
051 | var arrayMaker = { |
052 | someProperty: 'some value here' , |
053 | make: function (arg1, arg2) { |
054 | return [ this , arg1, arg2 ]; |
055 | } |
056 | }; |
057 | |
058 | |
059 | |
060 | 如果不把调用规则二 弄明白,那么在事件处理代码中 经常会遇到各种各样的bug,举个例子: |
061 | <input type= "button" value= "Button 1" id= "btn1" /> |
062 | <input type= "button" value= "Button 2" id= "btn2" /> |
063 | <input type= "button" value= "Button 3" id= "btn3" onclick= "buttonClicked();" /> |
064 | |
065 | < script type= "text/javascript" > |
066 | function buttonClicked(){ |
067 | var text = ( this === window) ? 'window' : this .id; |
068 | alert( text ); |
069 | } |
070 | var button1 = document.getElementById( 'btn1' ); |
071 | var button2 = document.getElementById( 'btn2' ); |
072 | |
073 | button1.onclick = buttonClicked; |
074 | button2.onclick = function (){ |
075 | buttonClicked(); |
076 | }; |
077 | < /script> |
078 | |
079 | 点击第一个按钮将会显示”btn1”,因为它是一个方法调用, this 为所属的对象(按钮元素) 。 |
080 | 点击第二个按钮将显示”window”,因为 buttonClicked 是被直接调用的( 不像 obj.buttonClicked() ), |
081 | 这和第三个按钮,将事件处理函数直接放在标签里是一样的.所以点击第三个按钮的结果是和第二个一样的。 |
082 | |
083 | 所以请大家注意: |
084 | button1.onclick = buttonClicked; |
085 | button2.onclick = function (){ |
086 | buttonClicked(); |
087 | }; |
088 | this 指向是有区别的。 |
089 | |
090 | |
091 | JavaScript函数调用规则三 |
092 | |
093 | 当然,如果使用的是jQuery库,那么你不必考虑这么多,它会帮助重写 this 的值以保证它包含了当前事件源元素的引用。 |
094 | //使用jQuery |
095 | $( '#btn1' ).click( function () { |
096 | alert( this .id ); // jQuery ensures 'this' will be the button |
097 | }); |
098 | |
099 | 那么 jQuery是如何重载 this 的值的呢? |
100 | 答案是: call()和apply(); |
101 | |
102 | 当函数使用的越来越多时,你会发现你需要的 this 并不在相同的上下文里,这样导致通讯起来异常困难。 |
103 | |
104 | 在Javascript中函数也是对象,函数对象包含一些预定义的方法,其中有两个便是apply()和call(),我们可以使用它们来对 this 进行上下文重置。 |
105 | |
106 | <input type= "button" value= "Button 1" id= "btn1" /> |
107 | <input type= "button" value= "Button 2" id= "btn2" /> |
108 | <input type= "button" value= "Button 3" id= "btn3" onclick= "buttonClicked();" /> |
109 | |
110 | < script type= "text/javascript" > |
111 | function buttonClicked(){ |
112 | var text = ( this === window) ? 'window' : this .id; |
113 | alert( text ); |
114 | } |
115 | var button1 = document.getElementById( 'btn1' ); |
116 | var button2 = document.getElementById( 'btn2' ); |
117 | |
118 | button1.onclick = buttonClicked; |
119 | button2.onclick = function (){ |
120 | buttonClicked.call( this ); // btn2 |
121 | }; |
122 | < /script> |
123 | |
124 | |
125 | |
126 | JavaScript函数调用规则四 |
127 | |
128 | (1)构造器 |
129 | 我不想深入研究在Javascript中类型的定义,但是在此刻我们需要知道在Javascript中没有类, |
130 | 而且任何一个自定义的类型需要一个初始化函数,使用原型对象(作为初始化函数的一个属性)定义你的类型也是一个不错的想法, |
131 | 让我们来创建一个简单的类型 |
132 | //声明一个构造器 |
133 | function ArrayMaker(arg1, arg2) { |
134 | this .someProperty = 'whatever' ; |
135 | this .theArray = [ this , arg1, arg2 ]; |
136 | } |
137 | // 声明实例化方法 |
138 | ArrayMaker.prototype = { |
139 | someMethod: function () { |
140 | alert( 'someMethod called' ); |
141 | }, |
142 | getArray: function () { |
143 | return this .theArray; |
144 | } |
145 | }; |
146 | |
147 | var am = new ArrayMaker( 'one' , 'two' ); |
148 | var other = new ArrayMaker( 'first' , 'second' ); |
149 | |
150 | am.getArray(); |
151 | // => [ am, 'one' , 'two' ] |
152 | other.getArray(); |
153 | // => [ other, 'first', 'second' ] |
154 | |
155 | 一个非常重要并值得注意的是出现在函数调用前面的 new 运算符,没有那个,你的函数就像全局函数一样,且我们创建的那些属性都将是创建在全局对象上(window),而你并不想那样。 |
156 | 另外一点,因为在你的构造器里没有返回值,所以如果你忘记使用 new 运算符,将导致你的一些变量被赋值为 undefined。 |
157 | |
158 | 所以构造器函数以大写字母开头是一个好的习惯,这可以作为一个提醒,让你在调用的时候不要忘记前面的 new 运算符. |
159 | 这样 初始化函数里的代码和你在其他语言里写的初始化函数是相似的. this 的值将是你将创建的对象. |
160 | |
161 | |
162 | |
163 | 总结 |
164 | 我希望通过这些来使你们理解各种函数调用方式的不同, |
165 | 让你的JavaScript代码远离bugs。 |
166 | 知道 this 的值是你避免bugs的第一步。 |