函数存储在变量中?Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11234000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Functions stored in variables? Javascript
提问by Apollo
Could somebody please explain what this notation is in javascript? What is function(d) doing? In this program it seemse that x is called by the following, but I have no idea what any of this means. Thanks in advance...
有人可以解释一下这个符号在javascript中是什么吗?函数(d)在做什么?在这个程序中,x 似乎被以下调用,但我不知道这意味着什么。提前致谢...
x = function(d) { return d.x * width / mx; };
// later....
x({x: .9}); // call
回答by Eswar Rajesh Pinapala
.9 is a value of the property x of the object(d) being passed into the function.
.9 是传递给函数的 object(d) 的属性 x 的值。
In the function, d = {x:9}(object) , now when you ask for d's property(x) Value (using DOT notation), it returns the value for the property x.
在函数 d = {x:9}(object) 中,现在当您请求 d 的 property(x) 值(使用 DOT 表示法)时,它返回属性 x 的值。
so d.x returns 0.9!
所以 dx 返回 0.9!
So you would ask me how did i pass the value of the property into the function-X in the first place, well thats what we did when we dis this -> x(objectBeingSent); where objectBeingSent is {x: .9}.
所以你会问我最初是如何将属性的值传递给函数-X 的,这就是我们在 dis this -> x(objectBeingSent); 时所做的。其中 objectBeingSent 是 {x: .9}。
Anonymous functions are functions that are dynamically declared at runtime. They're called anonymous functions because they aren't given a name in the same way as normal functions.
匿名函数是在运行时动态声明的函数。它们被称为匿名函数,因为它们不像普通函数那样被命名。
Anonymous functions are declared using the function operator. You can use the function operator to create a new function wherever it's valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.
匿名函数是使用函数运算符声明的。您可以使用函数运算符在可以放置表达式的任何位置创建新函数。例如,您可以将新函数声明为函数调用的参数或分配另一个对象的属性。
The function operator returns a reference to the function that was just created. The function can then be assigned to a variable, passed as a parameter or returned from another function. This is possible because functions are first class objects in javascript.
函数运算符返回对刚刚创建的函数的引用。然后可以将该函数分配给一个变量,作为参数传递或从另一个函数返回。这是可能的,因为函数是 javascript 中的第一类对象。
Here's an example where a function is declared in the regular way using the function statement:
这是一个使用 function 语句以常规方式声明函数的示例:
function eatCake(){
alert("So delicious and moist");
}
eatCake();
Here's an example where the same function is declared dynamically using the function operator:
这是一个使用函数运算符动态声明相同函数的示例:
var eatCakeAnon = function(){
alert("So delicious and moist");
};
eatCakeAnon();
See the semicolon after the second function's closing bracket? };You use a semi-colon after a statement. This is a statement:
看到第二个函数的右括号后的分号了吗?}; 在语句后使用分号。这是一个声明:
var eatCakeAnon = function(){
alert("So delicious and moist");
};
P.S. Best explanantion that i could find!
PS 我能找到的最好的解释!
回答by Dondi Michael Stroma
It's called an anonymous function. The function takes an object in the form of {x: number} as an argument and does some math on number.
它被称为匿名函数。该函数采用 {x: number}形式的对象作为参数,并对number 进行一些数学运算。