gpt4 book ai didi

JavaScript 语法

转载 作者:行者123 更新时间:2023-11-28 16:29:20 26 4
gpt4 key购买 nike

我的编程背景是在学校学过一点Java。由于某些原因,JavaScript 语法往往让我感到困惑。下面的 JavaScript 代码是一种我不知道如何构成的语法模式:

foo.ready = function(variable){... var whatever = variable.bar();

我的主要困惑点之一是参数。举一个对比示例,在 Java 中,如果我调用带有一个参数的方法,则该调用会发送一个参数。我不明白名为“variable”的参数是如何设置的。

最佳答案

变量在调用foo.ready时被分配,并传递一些值(可能是一个函数)。

var foo = {};  // 1. Create an object named "foo"

// 2. Assign a function to the "ready" property of "foo"
foo.ready = function(variable) {

// 4. "foo.ready" has been invoked, and received a function to its
// "variable" parameter, which it then invokes.
// The return value is assigned to "whatever"
var whatever = variable();

alert( whatever ); // 5. alert the value returned from the function

};

// 3. Invoke "foo.ready" passing a function as the argument.
// The function merely returns a string
foo.ready( function() { return "Hello world"; } );

这是一个工作示例: http://jsfiddle.net/69grz/

关于JavaScript 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6701379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com