简体   繁体   English

什么被传递到这个'函数(x)'?

[英]What is being passed into this 'function(x)'?

I'm having a hard time grasping some finer details of javascript, I have this function function(x) it doesn't seem to be receiving any parameters. 我很难掌握javascript的一些细节,我有这个函数function(x)它似乎没有接收任何参数。 Maybe I'm not understanding the Math.sin part. 也许我不理解Math.sin部分。 I'm not sure. 我不确定。 Any ideas on what is happening? 关于发生了什么的任何想法?

function makeDerivative( f, deltaX )
     {
        var deriv = function(x) // x isn't designated anywhere
        { 
           return ( f(x + deltaX) - f(x) )/ deltaX;
        }
        return deriv;
    }
    var cos = makeDerivative( Math.sin, 0.000001);
    // cos(0)     ~> 1
    // cos(pi/2)  ~> 0

Update I tried this instead and got NaN, then 15 更新我尝试了这个,然后获得NaN,然后​​是15

function addthings(x, y)
    {
                var addition = function(m)
          {
                return( x + y + m);
          }
          return addition;
    }

    var add = addthings(5, 5);
    alert(add());
    alert(add(5));

The outer function returns the inner function, so everything you pass to cos later theoretically is what you pass into the inner function. 外部函数返回内部函数,所以你在理论上传递给cos所有东西都是你传递给内部函数的东西。 Imagine calling it like this: 想象一下这样称呼它:

console.log(makeDerivative( Math.sin, 0.000001)(0)); // 1

would output the same as if you're doing it as described 输出就像你正如所描述的那样输出

console.log(cos(0)) // 1

as cos is assigned reference to a function (the one that gets returned by makeDerivative ). 因为cos被赋予对函数的引用(由makeDerivative返回的makeDerivative )。

要了解代码的工作原理,您必须阅读有关函数javascript函数闭包的更多信息

The other answers touch on the issue, but I'm going to try to get to the core of it. 其他答案触及了这个问题,但我将尝试找到它的核心。

JavaScript variables are untyped, which means that they can dynamically change what kind of variable they are. JavaScript变量是无类型的,这意味着它们可以动态地更改它们是什么类型的变量。 On a simple level, this means that var a can be instantiated as an array, and then later assigned as a string on the fly. 在一个简单的层面上,这意味着var a可以实例化为数组,然后在运行时将其指定为字符串。

But you can also store entire functions within those untyped variables. 但是您也可以在这些无类型变量中存储整个函数 For example; 例如;

var test = 'hey'
console.log(test)      //:: hey
test = function(input){ console.log('inner function: ' + input) }
console.log(test) //:: function(input){ console.log('inner function' + input) }
test() //:: inner function
test('with input') //:: inner function with input

Where //:: _____ represents the output to the console. 其中// :: _____表示控制台的输出。

So the function you are using returns another dynamic function. 所以你正在使用的函数返回另一个动态函数。 You can then call that function in a normal fashion, inputting the value for x when you call it. 然后您可以以正常方式调用函数,在调用它时输入x的值。

Well, you don't really call the function in the code you posted. 好吧,你并没有真正调用你发布的代码中的函数。

The makeDerivative gets reference to Math.sin function and inside it uses it to create reference to function which compute derivation of the passed function (see the f parameter). makeDerivative获取对Math.sin函数的引用,并在其中使用它来创建函数的引用,该函数计算传递函数的派生(参见f参数)。

In your example this reference is assigned to cos variable. 在您的示例中,此引用被分配给cos变量。 If you called it with ( 0 ) argument, you would get 1 as return value and the argument ( 0 ) would be passed into that deriv function... 如果你用( 0 )参数调用它,你会得到1作为返回值,参数( 0 )将传递给该派生函数...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM