使用 Firebug 和 jsfiddle.net 测试功能
我是一名新手程序员,从 Stoyan Stefanov 的面向对象 JavaScript 书中获得了以下函数。他说如果你调用 next 3 次,它将输出“a”和“b”,然后是“c”。当我在 firebug 控制台中尝试它时,它一直给我“a”,所以这是一个问题(a),即是否有关于 firebug 的内容可以解释我的结果?
接下来,我尝试在 jsfiddle.net 中运行它,但它不会输出任何内容。 http://jsfiddle.net/mjmitche/SkSMm/
我确信我
我做错了什么,但是什么?如果可以的话请解释一下。注意,我做了 next();得到了 A,然后我做了 next();再次得到 'a' 和 next();再次得到“a”。换句话说,计数器没有改变或不记得。
function setup(x) {
var i = 0;
return function () {
return x[i++];
};
}
var next = setup(['a','b','c']);
next();
Im a newbie programmer who got the function below from Stoyan Stefanovs object oriented JavaScript Book. He says that if you call next three times, it will output "a" and "b" and then "c". When I tried it in firebug console, it kept giving me "a", so that`s one question (a) i.e. is there something about firebug that would explain my result?
Next, I tried to run it in jsfiddle.net but it won`t output anything. http://jsfiddle.net/mjmitche/SkSMm/
Im sure I
m doing something wrong, but what? Please explain if you can. Note, I did next(); and got A, and then I did next(); again and got 'a' and next(); again and got 'a'. In other words, the counter didnt change or didn
t remember.
function setup(x) {
var i = 0;
return function () {
return x[i++];
};
}
var next = setup(['a','b','c']);
next();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是显示其工作原理的 jsfiddle 链接:
http://jsfiddle.net/ZnZTk/
Here is the jsfiddle link to show it works:
http://jsfiddle.net/ZnZTk/
JsFiddle 与控制台不同,它没有输出返回值的窗口。代码的结果是一个网页,如右下角所示。
您可以使用
alert
方法来显示值:http://jsfiddle.net/ SkSMm/4/
如您所见,调用
next
三次实际上会输出数组中的三个值。setup
函数将委托返回到在函数中创建的匿名函数。由于该匿名函数使用其自身外部的变量,但这些变量对于周围函数来说是本地的,因此为该函数创建了一个闭包。闭包将包含i
和x
变量。由于闭包属于委托,因此它将在一个函数调用到下一个函数调用中保留下来,并保留其变量的值。您可以仅使用全局变量做类似的事情:
由于变量是在函数外部声明的,因此它们将在函数调用之间保留。
使用全局变量的缺点是,如果没有为变量指定非常唯一的名称,则一个脚本很容易与另一个脚本发生冲突。如果使用闭包,则不存在一个脚本的变量与另一脚本的变量冲突的风险。
JsFiddle is not like the console, it doesn't have a window where it will output return values. The result of the code is a web page, that is shown at the lower right.
You can use the
alert
method to show the values:http://jsfiddle.net/SkSMm/4/
As you see, calling
next
three times will actually output the three values in the array. Thesetup
function returns a delegate to the anonumous function that is created in the function. As that anonymous function uses variables outside itself, but which are local to the surrounding function, a closure is created for the function. The closure will contain thei
andx
variables. As the closure belongs to the delegate, it will survive from one function call to the next, and retain the values of it's variables.You could do a similar thing just using global variables:
As the variables are declared outside the function, they will survive between the function calls.
The drawback of using global variables is that one script easily clashes with another if the variables are not given very unique names. If you use a closure, there is no risk of the variables of one script to conflict with variables of another script.
你做错了:
和 jsfiddle: http://jsfiddle.net/ZHgW2/
You did it wrong:
And jsfiddle: http://jsfiddle.net/ZHgW2/
这是一个简洁的演示,它利用了导入的
say
函数 并依赖于一个按钮:http://jsfiddle.net/entropo/wxTqR/
这是这是一种无需依赖日志或警报即可测试脚本的好方法。
say
函数来自 jQuery in Action。摘录:Here's a neat demo that takes advantage of an imported
say
function and relies on a button:http://jsfiddle.net/entropo/wxTqR/
This is a great way to test your scripts without relying on the log or alerts.
The
say
function is from jQuery in Action. Excerpt: