Is it true that a closure is created in the following cases for foo , but not for bar ? Case 1: <script type="text/javascript"> function foo() { } </script> foo is a closure with a scope chain with only the global scope. Case 2: <script type="text/javascript"> var i = 1; function foo() { return i; } </script> same as Case 1. Case 3: <script ty
这是真的,一个是封闭在下列情况下为创建foo ,但不适合bar ? 情况1: <script type="text/javascript"> function foo() { } </script> foo是一个仅具有全局范围的范围链的闭包。 案例2: <script type="text/javascript"> var i = 1; function foo() { return i; } </script> 与案例1相同。 案例3: <script type="text/javascript"> function Circle(r) {
Possible Duplicate: Location of parenthesis for auto-executing anonymous JavaScript functions? Sometimes I see: (function() { ... }()); and sometimes I see: (function() { ... })(); I see both forms with and without arguments. They both execute the anonymous function. Is there a difference between the two forms? Are there any compelling reasons to use one form over the other? There
可能重复: 自动执行匿名JavaScript函数的括号位置? 有时我看到: (function() { ... }()); 有时我会看到: (function() { ... })(); 我看到两种形式都有和没有参数。 他们都 执行匿名函数。 这两种形式有什么区别吗? 是否有任何令人信服的理由使用另一种形式? 这两种形式没有实际区别,但从语法角度来看,两者的区别在于,分组操作符 - 括号 - 在第一个示例CallExpression包含CallExpression ,其中包含Func
I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed. The code used to wrap an anonymous function in parenthesis and then execute it, (function () { // code here })(); but now it wraps the auto-executed function in parenthesis. (function () { // code here }());
我最近将json2.js的当前版本与我在项目中使用的版本进行了比较,并注意到函数表达式的创建方式和自我执行方式存在差异。 用于将括号中的匿名函数包装并执行的代码, (function () { // code here })(); 但现在它将自动执行的功能封装在括号中。 (function () { // code here }()); 在解释JavaScript封装的匿名函数语法的接受答案中,CMS提出了一个评论:“ (function(){})(); 和(function(){}()); 是有效的“。 我想
Javascript closure definition says : A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression). Can some one explain to me the concept of free variables ? Is this concept Javascript specific or applies to other languages also ? Free variables are simply the vari
Javascript关闭定义说: “闭包”是一个表达式(通常是一个函数),可以将自由变量与一个绑定这些变量的环境(即“关闭”表达式)一起使用。 有人可以向我解释自由变量的概念吗? 这个概念是Javascript的特定还是适用于其他语言? 自由变量只是本地声明或作为参数传递的变量。 资源 : 在计算机编程中,术语自由变量指的是函数中使用的变量,它们不是局部变量,也不是该函数的参数.1术语非局部变量通常在此上下文中是同义
My intuition is that it's a good idea to encapsulate blocks of code in anonymous functions like this: (function() { var aVar; aVar.func = function() { alert('ronk'); }; aVar.mem = 5; })(); Because I'm not going to need aVar again, so I assume that the garbage collector will then delete aVar when it goes out of scope. Is this right? Or are interpreters smart enough to see that I
我的直觉是,将代码块封装在匿名函数中是个好主意,例如: (function() { var aVar; aVar.func = function() { alert('ronk'); }; aVar.mem = 5; })(); 因为我不再需要aVar了,所以我认为垃圾收集器会在超出范围时删除aVar 。 这是正确的吗? 还是口译员很聪明,看到我不再使用该变量并立即清理它? 是否有任何理由,如风格或可读性,我不应该使用匿名函数这种方式? 另外,如果我命名函数,如下所示: var operati
Suppose I have some Ext JS code that attaches an event handler to all elements with class "myclass". For example: Ext.onReady(function(){ Ext.select('a[class*=myclass]').on('click', function(event, elm){ // do something }); }); And my html might be as follows: <a class="myclass" href="#">test1</a> <a class="myclass" href="#">test2</a> <a class=
假设我有一些Ext JS代码将一个事件处理程序附加到类“myclass”的所有元素上。 例如: Ext.onReady(function(){ Ext.select('a[class*=myclass]').on('click', function(event, elm){ // do something }); }); 我的html可能如下所示: <a class="myclass" href="#">test1</a> <a class="myclass" href="#">test2</a> <a class="myclass" href="#">test3</a> 这没有问题。
I am trying to figure out how to bind an event to dynamically created elements. I need the event to persist on the element even after it is destroyed and regenerated. Obviously with jQuery's live function its easy, but what would they look like implemented with native Javascript? Here's a simple example: function live(eventType, elementId, cb) { document.addEventListener(eventTy
我想弄清楚如何将一个事件绑定到动态创建的元素。 即使在它被破坏和再生之后,我也需要这个事件坚持这个元素。 很明显,jQuery的live函数很简单,但是它们看起来像用原生Javascript实现的那样? 这里有一个简单的例子: function live(eventType, elementId, cb) { document.addEventListener(eventType, function (event) { if (event.target.id === elementId) { cb.call(event.target, event);
Where this is coming from When I first learned jQuery, I normally attached events like this: $('.my-widget a').click(function() { $(this).toggleClass('active'); }); After learning more about selector speed and event delegation, I read in several places that "jQuery event delegation will make your code faster." So I started to write code like this: $('.my-widget').on('click','a
这是从哪里来的 当我第一次学习jQuery时,我通常会附加这样的事件: $('.my-widget a').click(function() { $(this).toggleClass('active'); }); 在了解了关于选择器速度和事件委托的更多信息后,我在几处地方读到“jQuery事件委托会让你的代码更快。” 所以我开始写这样的代码: $('.my-widget').on('click','a',function() { $(this).toggleClass('active'); }); 这也是复制已弃用的.live()事件行为的推荐方法
With jQuery, how do I find out which key was pressed when I bind to the keypress event? $('#searchbox input').bind('keypress', function(e) {}); I want to trigger a submit when ENTER is pressed. [Update] Even though I found the (or better: one) answer myself, there seems to be some room for variation ;) Is there a difference between keyCode and which - especially if I'm just looking fo
使用jQuery,当我绑定到按键事件时,如何查找哪个按键被按下? $('#searchbox input').bind('keypress', function(e) {}); 我想在按下ENTER时触发提交。 [更新] 即使我发现(或更好:一个)回答我自己,似乎有一些变化的空间;) keyCode和which一个之间有区别 - 特别是如果我只是在寻找不会是unicode关键字的ENTER? 做一些浏览器提供一个属性,其他浏览器提供另一个? 其实这是更好的: var code = e.keyCode || e.w
I'm looking for a JavaScript library that will allow me to query complex JSON objects using a LINQ-like syntax. A quick search found a couple of promising options that look they might offer what I need: LINQ to JavaScript and jLinq Does any one have any experience using them? What are some pros and cons? Is the performance comparable? Does the function-passing syntax of LINQ to Jav
我正在寻找一个JavaScript库,它允许我使用类似LINQ的语法来查询复杂的JSON对象。 快速搜索发现了几个有前途的选项,看起来他们可能提供我需要的东西: LINQ to JavaScript和jLinq 有没有人有使用它们的经验? 有什么优点和缺点? 性能是否可比? LINQ to JavaScript的函数传递语法是否提供了任何隐藏的好处(我个人首先发现jLinq的语法更具吸引力)? 这两个项目都缺乏什么? 你有没有尝试联系作者? 他们的反