g*****g 发帖数: 34805 | 1 YAHOO.example.FnMultipleFields = function() {
var oDS = new YAHOO.util.FunctionDataSource(matchNames);
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
return {
oDS : oDS,
oAC : oAC
};
}();
FnMultiFields 到底是个类还是个变量?这个return什么意思,还有最后的括号。
或者这就像java的匿名类? |
m******t 发帖数: 2416 | 2
It's a variable holding a reference to the function defined here.
Similar to the function pointer in C, or the (upcoming) closure
in Java.
Return returns the return value of the function, which is an object/map
defined on the fly with two properties.
The last () is also part of the function declaration.
【在 g*****g 的大作中提到】 : YAHOO.example.FnMultipleFields = function() { : var oDS = new YAHOO.util.FunctionDataSource(matchNames); : var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS); : return { : oDS : oDS, : oAC : oAC : }; : }(); : FnMultiFields 到底是个类还是个变量?这个return什么意思,还有最后的括号。 : 或者这就像java的匿名类?
|
g*****g 发帖数: 34805 | 3 最后这个()还是不明白,定义一个变量指向函数我理解。
但不应该是
xx = function(){return ....};就够了?
【在 m******t 的大作中提到】 : : It's a variable holding a reference to the function defined here. : Similar to the function pointer in C, or the (upcoming) closure : in Java. : Return returns the return value of the function, which is an object/map : defined on the fly with two properties. : The last () is also part of the function declaration.
|
m******t 发帖数: 2416 | 4
Heh, to be honest, I don't quite get it either. I'm sort of
just taking it for granted that it's just how javascript
works.
What's the javascript guru around here?
【在 g*****g 的大作中提到】 : 最后这个()还是不明白,定义一个变量指向函数我理解。 : 但不应该是 : xx = function(){return ....};就够了?
|
s******n 发帖数: 876 | 5 invoking the function, duh!
【在 m******t 的大作中提到】 : : Heh, to be honest, I don't quite get it either. I'm sort of : just taking it for granted that it's just how javascript : works. : What's the javascript guru around here?
|
g*****g 发帖数: 34805 | 6 revisiting it, it's probably equivalent to
var temp = function(){};
YAHOO.example.FnMultipleFields = temp();
So YAHOO.example.FnMultipleFields is not a function pointer, but the return
value of that function. Now it makes more sense. It's like anonymous inner
class in Java, the object is never named but an instance is created.
【在 m******t 的大作中提到】 : : Heh, to be honest, I don't quite get it either. I'm sort of : just taking it for granted that it's just how javascript : works. : What's the javascript guru around here?
|
g**e 发帖数: 6127 | 7 好虫解释的好
return
【在 g*****g 的大作中提到】 : revisiting it, it's probably equivalent to : var temp = function(){}; : YAHOO.example.FnMultipleFields = temp(); : So YAHOO.example.FnMultipleFields is not a function pointer, but the return : value of that function. Now it makes more sense. It's like anonymous inner : class in Java, the object is never named but an instance is created.
|
m******t 发帖数: 2416 | 8
LOL, duh exactly.
【在 s******n 的大作中提到】 : invoking the function, duh!
|
h*********o 发帖数: 62 | 9 YAHOO.example.FnMultipleFields is a js static variable.
without extra (), it would be a definition for a static function that
returns a Jason type object. |
X****r 发帖数: 3557 | 10 Exactly. The code is almost the same as
var oDS = new YAHOO.util.FunctionDataSource(matchNames);
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
YAHOO.example.FnMultipleFields = {oDS : oDS, oAC : oAC};
Except that the variable oDS and oAC are never introduced in the global
(or current) scope.
return
inner
【在 g*****g 的大作中提到】 : revisiting it, it's probably equivalent to : var temp = function(){}; : YAHOO.example.FnMultipleFields = temp(); : So YAHOO.example.FnMultipleFields is not a function pointer, but the return : value of that function. Now it makes more sense. It's like anonymous inner : class in Java, the object is never named but an instance is created.
|