Please explain this Javascript closure exercise

This question already has an answer here: How do JavaScript closures work? 88 answers In order to understand this you must know what is the difference between a function call and a reference to a function. As well as how scopes work in javascript. Assuming you do know these things, let's get explaining. So you first have a variable hidden that is being assigned a value of mystery(3)

请解释这个Javascript关闭练习

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 为了理解这个,你必须知道函数调用和函数引用之间有什么区别。 以及范围如何在JavaScript中工作。 假设你知道这些事情,让我们来解释一下。 所以你首先有一个hidden的变量,被赋予一个mystery(3)的值mystery(3) 。 所以立即看看函数的mystery ,看看它返回的结果。 它返回一个内部函数mystery2的引用 。 所以现在hidden了一个引用 ,这意味着它没

Confused by closures in JavaScript

Possible Duplicate: How do JavaScript closures work? Javascript closures and side effects in plain English? (separately) I'm new to JavaScript but I'm really confused by how closures work. Can someone explain in layman's terms what they are or why they are useful? Closures are something like the context of a function when it is defined. Whenever a function is defined, the c

被JavaScript中的闭包困惑

可能重复: JavaScript关闭如何工作? 简单的英语JavaScript封闭和副作用? (分别) 我是JavaScript的新手,但我真的很关心闭包的工作方式。 有人可以用外行的话来解释他们是什么或为什么他们有用吗? 闭包在定义时就像函数的上下文一样。 每当一个函数被定义时,上下文就会被存储,并且即使函数的“正常”生命周期结束了,如果你保持对函数执行时定义的元素的引用,它仍然可以访问上下文的元素闭包),这实际上是你的

Explain the following JavaScript statement?

This question already has an answer here: How do JavaScript closures work? 88 answers Why do you need to invoke an anonymous function on the same line? 19 answers This code is equivalent to: function Ninja() { // nothing here } var ninja = new Ninja(); Though in the code you listed, the function/object Ninja is not global scope. The code (function() {...})(); basically says &quo

请解释下面的JavaScript语句?

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 为什么你需要在同一行上调用匿名函数? 19个答案 此代码相当于: function Ninja() { // nothing here } var ninja = new Ninja(); 尽管在你列出的代码中,函数/对象Ninja不是全局范围。 代码(function() {...})(); 基本上说“把这里面包含的任何函数都包含进来,然后立即执行”。 所以它创建了一个匿名函数并在之后调用它。 它被称为立即调

What is wrong with my javascript scope?

This question already has an answer here: How do JavaScript closures work? 88 answers JavaScript closure inside loops – simple practical example 35 answers Javascript has function scope. This means that for(...) { var j = i; } is equivalent to var j; for(...) { j = i; } In fact, this is how Javascript compilers will actually treat this code. And, of course, this causes your l

我的JavaScript范围有什么问题?

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 循环中的JavaScript闭包 - 简单实用的例子35个答案 Javascript有功能范围。 这意味着 for(...) { var j = i; } 相当于 var j; for(...) { j = i; } 事实上,这是JavaScript编译器如何处理这些代码。 当然,这会导致你的小技巧失败,因为在setTimeout的函数被调用之前, j将会增加,即j现在不会做任何与i不同的任何事情,它只是一个具有相

Awkward way of executing JavaScript code

This question already has an answer here: How do JavaScript closures work? 88 answers Why is this function wrapped in parentheses, followed by parentheses? [duplicate] 6 answers This is a poor example. Consider the following: var a = (function(){ var ret = {}; ret.test = "123"; function imPrivate() { /* ... */ } ret.public = function() { imPrivate(); } return ret; })

执行JavaScript代码的尴尬方式

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 为什么这个函数用圆括号括起来,然后用括号括起来? [复制] 6个回答 这是一个不好的例子。 考虑以下: var a = (function(){ var ret = {}; ret.test = "123"; function imPrivate() { /* ... */ } ret.public = function() { imPrivate(); } return ret; })(); a将包含varible test和public function,但不能访问imPrivate。

Javascript local and global variable confusion

This question already has an answer here: How do JavaScript closures work? 88 answers Variable: local scope, global scope or is it the JavaScript engine? 3 answers In Javascript, the variable declarations are automatically moved to the top of the function. So, the interpreter would make it look more like this: var myname = "initial" function c(){ var myname; // alerts undefined

Javascript本地和全局变量混淆

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 变量:局部范围,全局范围还是JavaScript引擎? 3个答案 在Javascript中,变量声明自动移动到函数的顶部。 所以,解释者会让它看起来更像这样: var myname = "initial" function c(){ var myname; // alerts undefined alert(myname); myname = "changed"; // alerts changed alert(myname); } c(); 这被称为'提升'

JavaScript click handler not working as expected inside a for loop

This question already has an answer here: How do JavaScript closures work? 88 answers Working DEMO This is a classic JavaScript closure problem. Reference to the i object is being stored in the click handler closure, rather than the actual value of i . Every single click handler will refer to the same object because there's only one counter object which holds 6 so you get six on eac

JavaScript点击处理程序在for循环中没有像预期的那样工作

这个问题在这里已经有了答案: JavaScript关闭如何工作? 88个答案 工作DEMO 这是一个典型的JavaScript关闭问题。 i对象的引用存储在click处理程序闭包中,而不是i的实际值。 每个单击处理程序都会引用同一个对象,因为只有一个可以保留6的计数器对象,所以每次点击时都会得到6个计数器对象。 解决方法是将其封装在一个匿名函数中,并将其作为参数传递给我。 基元在函数调用中通过值复制。 for(var i=1; i<6; i+

Streamlining my javascript with a function

i have a series of select lists, that i am using to populate text boxes with ids. so you click a select option and another text box is filled with its id. with just one select/id pair this works fine, but i have multiples, and the only thing that changes is the id of the select and input.. in fact just the ending changes, the inputs all start with featredproductid and the select ids all start

简化我的JavaScript与功能

我有一系列选择列表,我正在使用ID填充文本框。 所以你点击一个选择选项,另一个文本框被填入其ID。 只有一个选择/ ID对这工作得很好,但我有倍数,唯一改变的是选择和输入的ID ..实际上只是结局的变化,输入全部以featredproductid和选择ID开头先从食谱产品开始,然后以类别结束。 我知道,为每个类别反复列出这一点并不是做到这一点的方法。 我想我需要制作一系列类别var cats = ['橄榄油','谷物',

Detect the specific iPhone/iPod touch model in Javascript

I am developing a website for Apple i* devices which uses HTML5 webkit features like transitions but I want to disable some of the fancy stuff for older/slower iPhone models like iPhone < 3GS and iPod touch < 3rd Gen because on those devices the transitions are too slow. Is there a way to detect the exact model (not just the OS/User Agent) within Javascript? 这似乎工作,至少要确定2至3克

使用Javascript检测特定的iPhone / iPod touch模型

我正在为Apple i *设备开发一个网站,该网站使用HTML5 webkit功能(如转换),但我希望禁用iPhone <3GS和iPod touch <第三代等较老/较慢iPhone模型的某些花哨材料,因为在这些设备上转换太慢了。 有没有办法在Javascript中检测确切的模型(不仅仅是操作系统/用户代理)? 这似乎工作,至少要确定2至3克和3克...不知道如何确定它是否是4克尚未... var percentmobile_t = new Date().getTime(); var percentmobile_s = 0;

How to run a jQuery function after all and any other javascript has run

I have a photo gallery page hosted on a CMS (Squarespace) which has some of it's own scripts which load the thumbnails asynchronously. The actual large images however are not preloaded, so I decided to add my own script into the mix to just make the browser load those larger images into the cache in the background, like this: (function($) { var cache = []; // Arguments are image paths

如何运行一个jQuery函数和所有其他的JavaScript运行

我有一个托管在CMS(Squarespace)上的照片库页面,该页面有一些它自己的脚本,可以异步加载缩略图。 然而,实际的大图像并未预先加载,因此我决定将自己的脚本添加到混合中,以使浏览器将较大的图像加载到后台的缓存中,如下所示: (function($) { var cache = []; // Arguments are image paths relative to the current page. $.preLoadImages = function() { var args_len = arguments.length; for (var i =