Minimum viable JavaScript closure

The top answer on Stack Overflow regarding JavaScript closures defines them as (paraphrase): A function that simply accesses variables outside of your immediate lexical scope that are not deallocated after the function returns. Based strictly on this definition it seems like we can reduce the minimum viable closure (that does something useful) to a function containing no local variables that

最小可行的JavaScript关闭

Stack Overflow关于JavaScript关闭的最佳答案将它们定义为(释义): 一个函数,它简单地访问您的直接词法作用域之外的变量,这些变量在函数返回后不会被释放。 严格地基于这个定义,似乎我们可以减少最小可行闭包(做一些有用的事情)到一个不包含局部变量的函数,该局部变量会改变全局范围中的某些东西: var x = 0; function foo() { x = x + 1; return x; } foo(); // returns 1 foo(); // returns 2, et cetera

What exactly does "closure" refer to in JavaScript?

I understand what closures are, but I am having some trouble grokking exactly what the term closure refers to. I have seen the term used in many websites, but rarely do they agree on the actual definition of it. Is it the variables that are kept on the stack frame? Is it the function that is being returned? Is it the scope of the outer function? Is it the scope of the inner (returned) fu

“闭包”究竟在JavaScript中提到了什么?

我明白什么是封闭,但我在closure术语所指的术语方面遇到了一些麻烦。 我在许多网站上看到了这个术语,但他们很少就它的实际定义达成一致。 它是保留在堆栈帧的变量吗? 它是被返回的函数吗? 它是外部函数的范围吗? 它是内部(返回)函数的范围吗? 这可能是在返回函数后将变量保留在栈帧上的概念吗? 有人能准确地告诉我什么closure是指? 来自JavaScript闭包 两个一句话总结: 闭包是函数的局部变量 - 在

setTimeout or setInterval?

As far as I can tell, these two pieces of javascript behave the same way: Option A: function myTimeoutFunction() { doStuff(); setTimeout(myTimeoutFunction, 1000); } myTimeoutFunction(); Option B: function myTimeoutFunction() { doStuff(); } myTimeoutFunction(); setInterval(myTimeoutFunction, 1000); Is there any difference between using setTimeout and setInterval? They essenti

setTimeout或setInterval?

据我所知,这两个JavaScript的行为方式相同: 选项A: function myTimeoutFunction() { doStuff(); setTimeout(myTimeoutFunction, 1000); } myTimeoutFunction(); 选项B: function myTimeoutFunction() { doStuff(); } myTimeoutFunction(); setInterval(myTimeoutFunction, 1000); 使用setTimeout和setInterval有什么区别吗? 他们基本上试图做同样的事情,但setInterval方法将比setTimeout方法更准确

Stop setInterval call in JavaScript

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event? I want the user to be able to stop the repeated refresh of data. setInterval() returns an interval ID, which you can pass to clearInterval() : var refreshIntervalId = setInterval(fname, 10000); /* later */ clearInterval(refreshIntervalId); See the docs

在JavaScript中停止setInterval调用

我正在使用setInterval(fname, 10000); 在JavaScript中每10秒调用一次函数。 是否有可能停止在某些事件上调用它? 我希望用户能够停止重复刷新数据。 setInterval()返回一个间隔ID,您可以将其传递给clearInterval() : var refreshIntervalId = setInterval(fname, 10000); /* later */ clearInterval(refreshIntervalId); 请参阅setInterval()和clearInterval()的文档。 如果将setInterval的返回值设置为变量,则可以

javascript closure advantages?

Whats the main purpose of Closures in JS. Is it just used for public and private variables? or is there something else that I missed. I am trying to understand closure and really want to know what are the main advantages of using it. I think the best phrase to sum up the purpose of closures would be: Data Encapsulation With a function closure you can store data in a separate scope, and s

JavaScript关闭优势?

什么是JS中闭包的主要目的。 它只是用于公共和私有变量吗? 还是有我错过的其他东西。 我想了解关闭,并且真的想知道使用它的主要优点。 我认为总结封锁目的的最佳措辞是: 数据封装 通过函数闭包,您可以将数据存储在单独的作用域中,并仅在必要时共享。 如果你想模拟private static variables ,你可以在一个函数中定义一个类,并在闭包中定义private static vars : (function () { var foo; foo = 0;

How does the delay parameter of setTimout() work within a loop?

I have the following code that involves an IIFE (immediately invoking function expression) within a for loop. The IIFE function is clearly getting the right parameter as the printout is as expected. But I don't understand what the interval is doing. As far as I can tell the interval for the 1st iteration should be 1 sec, then 2 sec for the 2nd iteration, etc. etc. for (var i = 1; i <=

setTimout()的延迟参数如何在循环中工作?

我有以下代码,涉及一个for循环内的IIFE(立即调用函数表达式)。 由于打印输出如预期,IIFE功能显然获得了正确的参数。 但我不明白间隔在做什么。 据我所知,第一次迭代的间隔应该是1秒,然后是第二次迭代的2秒等等。 for (var i = 1; i <= 5; i++) { (function(i){ setTimeout(function timer(){ console.log(i); }, i*1000); })(i); } 我看到i以1秒5的间隔打印出来。 这让我

If all JavaScript types are objects, then why are numbers be passed by value?

In articles about closures, you will often see closures being created inside of loops using self-invoking functions to pass the iterator variable to a returned function expression in order to create a closure around the value of the iterator variable at the time the self-invoking function was invoked, rather than its value after the loop finishes. Here is an example: var func = []; for (var i

如果所有JavaScript类型都是对象,那么为什么数字是按值传递的?

在关于闭包的文章中,您经常会看到使用自调用函数将闭包创建到循环内部,以便将迭代变量传递给返回的函数表达式,以便在自调用时创建围绕迭代变量值的闭包函数被调用,而不是循环结束后的值。 这里是一个例子: var func = []; for (var i = 0; i < 3; i++) { func.push((function(j){ return function(){ console.log(j); }; })(i)); } // Logs // 0 // 1 // 2 // to the console for (var i = 0; i < func.leng

Why is setTimeout executing timeExpired with no delay?

I am learning javascript, and have come across a problem that leaves me completely stumped. When I call setTimeout the function parameter runs immediately with no delay. Overall program (segment) purpose: User inputs a number of minutes they want to set a timer for. Inputed value is passed to a submitName(), which sets the timer and calls timeExpired() when the timer goes off. Relevant HTML

为什么setTimeout无延迟地执行timeExpired?

我正在学习JavaScript,并且遇到了一个让我完全陷入困境的问题。 当我调用setTimeout时,函数参数立即运行,没有任何延迟。 整体程序(分段)目的:用户输入他们想为其设置计时器的分钟数。 输入的值传递给一个submitName(),它设置定时器并在定时器关闭时调用timeExpired()。 相关HTML: <form id="frm1"> Name: <input type="number" id="fname" name="fname" step="1" min="1" max="1500" value="1">&l

JavaScript Nested function

I got a piece of code for javascript which I just do not understand: function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear(); } function outerFunc(base) { var punc = "!"; //inner function function returnString(ext) { return base + ext +

JavaScript嵌套功能

我得到了一段JavaScript代码,我只是不明白: function dmy(d) { function pad2(n) { return (n < 10) ? '0' + n : n; } return pad2(d.getUTCDate()) + '/' + pad2(d.getUTCMonth() + 1) + '/' + d.getUTCFullYear(); } function outerFunc(base) { var punc = "!"; //inner function function returnString(ext) { return base + ext + punc; } return r

What does it mean global namespace would be polluted?

What does it mean global namespace would be polluted? I don't really understand what global namespace getting polluted means. Quick Note On Garbage Collection As variables lose scope, they will be eligible for garbage collection. If they are scoped globally, then they will not be eligible for collection until the global namespace loses scope. Here is an example: var arra = []; for (

这意味着全球命名空间会受到什么污染?

这意味着全球命名空间会受到什么污染? 我不太明白全球命名空间被污染的含义。 关于垃圾收集的快速笔记 随着变量丢失范围,他们将有资格进行垃圾回收。 如果它们是全局范围的,那么在全局命名空间失去范围之前它们将不具备收集资格。 这里是一个例子: var arra = []; for (var i = 0; i < 2003000; i++) { arra.push(i * i + i); } 把这个添加到你的全局命名空间(至少对我来说)应该会增加10,000 kb的内存使用