使用 Firebug 和 jsfiddle.net 测试功能

发布于 2024-11-01 16:30:07 字数 600 浏览 0 评论 0原文

我是一名新手程序员,从 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 Im 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 didnt remember.

function setup(x) {
   var i = 0;
   return function () {
        return x[i++];
    };
}

var next = setup(['a','b','c']);

next();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肥爪爪 2024-11-08 16:30:07

这是显示其工作原理的 jsfiddle 链接:

http://jsfiddle.net/ZnZTk/

Here is the jsfiddle link to show it works:

http://jsfiddle.net/ZnZTk/

浮生面具三千个 2024-11-08 16:30:07

JsFiddle 与控制台不同,它没有输出返回值的窗口。代码的结果是一个网页,如右下角所示。

您可以使用 alert 方法来显示值:

alert(next());

http://jsfiddle.net/ SkSMm/4/

如您所见,调用 next 三次实际上会输出数组中的三个值。 setup 函数将委托返回到在函数中创建的匿名函数。由于该匿名函数使用其自身外部的变量,但这些变量对于周围函数来说是本地的,因此为该函数创建了一个闭包。闭包将包含 ix 变量。由于闭包属于委托,因此它将在一个函数调用到下一个函数调用中保留下来,并保留其变量的值。

您可以仅使用全局变量做类似的事情:

var x = ['a','b','c'];
var i = 0;

function next() {
  return x[i++];
}

alert(next());
alert(next());
alert(next());

由于变量是在函数外部声明的,因此它们将在函数调用之间保留。

使用全局变量的缺点是,如果没有为变量指定非常唯一的名称,则一个脚本很容易与另一个脚本发生冲突。如果使用闭包,则不存在一个脚本的变量与另一脚本的变量冲突的风险。

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:

alert(next());

http://jsfiddle.net/SkSMm/4/

As you see, calling next three times will actually output the three values in the array. The setup 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 the i and x 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:

var x = ['a','b','c'];
var i = 0;

function next() {
  return x[i++];
}

alert(next());
alert(next());
alert(next());

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.

辞别 2024-11-08 16:30:07

你做错了:
在此处输入图像描述

和 jsfiddle: http://jsfiddle.net/ZHgW2/

You did it wrong:
enter image description here

And jsfiddle: http://jsfiddle.net/ZHgW2/

小草泠泠 2024-11-08 16:30:07

这是一个简洁的演示,它利用了导入的 say 函数 并依赖于一个按钮:

http://jsfiddle.net/entropo/wxTqR/

这是这是一种无需依赖日志或警报即可测试脚本的好方法。

say 函数来自 jQuery in Action。摘录:

在这个函数中,我们使用了一个小实用函数 say() C 的服务,
我们用来向页面上动态创建的元素发送文本消息
我们称之为“控制台”。该函数在导入的支持中声明
脚本文件(jqia2.support.js),并且将省去我们使用烦人的和破坏性的警报来指示页面上发生的事情的麻烦。我们将在本书其余部分的许多示例中使用这个方便的函数。

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:

Within this function, we employ the services of a small utility function, say() C,
that we use to emit text messages to a dynamically created element on the page
that we’ll call the “console.” This function is declared within the imported support
script file (jqia2.support.js), and will save us the trouble of using annoying and disruptive alerts to indicate when things happen on our page. We’ll be using this handy function in many of the examples throughout the remainder of the book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文