存储在变量中的函数?使用Javascript [英] Functions stored in variables? Javascript

查看:73
本文介绍了存储在变量中的函数?使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下javascript中的这种表示法吗?什么是功能(d)在做什么?在这个程序中,似乎x被以下调用,但我不知道这意味着什么。提前致谢...

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...

x = function(d) { return d.x * width / mx; };

// later....
 x({x: .9}); // call


推荐答案

.9是属性的值传递给函数的对象(d)的x。

.9 is a value of the property x of the object(d) being passed into the function.

在函数中,d = {x:9}(对象),现在当你要求d属性时(x)值(使用DOT表示法),它返回属性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返回0.9!

so d.x returns 0.9!

所以你会问我如何将属性的值首先传递给函数-X,这就是我们在解决这个问题时所做的事情 - > X(objectBeingSent);其中objectBeingSent是{x:.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}.

匿名函数是在
运行时动态声明的函数。它们被称为匿名函数,因为它们没有以与普通函数相同的方式给
一个名称。

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.

使用函数运算符声明匿名函数。您可以使用函数运算符
创建一个新函数,只要它有效
来放置表达式。例如,您可以将新函数声明为函数调用的
a参数或指定另一个
对象的属性。

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.

函数运算符返回对刚刚创建的
函数的引用。然后可以将该函数分配给变量,将
作为参数传递或从另一个函数返回。这可能是
,因为函数是javascript中的第一类对象。

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.

这是一个使用函数以常规方式
声明函数的示例声明:

Here’s an example where a function is declared in the regular way using the function statement:

 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");
     };

来源

PS我能找到的最佳解释!

P.S. Best explanantion that i could find!

这篇关于存储在变量中的函数?使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆