本文介绍了JavaScript关闭的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 伙计们,我被困在下面的代码。首先我将描述用例:使用ColorGradient的实例调用函数addPreset。当调用 this.listController.addItem(...)一个名为 onSelect ist的回调函数时,每次触发listController-item上的onSelect-我想要做的是将对 GLab.ColorSlider.applyColorGradient(...)的调用包装到一个新的闭包中,这样 addPreset的cg参数* 问题:现在每次 addPreset 被调用时, cg (通过调用传递)将覆盖之前分配的所有值,但 this.presetList 始终包含正确的值 请帮助我::-) $关闭函数b $ b 感谢,到目前为止 function addPreset(cg){ if(! ColorGradient)){ throw new TypeError(PresetManager:Can not add preset; invalid arguments received); } var newIndex = this.listController.addItem(cg.getName (),{ onSelect:(function(cg2){ return function(){ // addPreset的作用域现在应该被破坏 GLab.ColorSlider.applyColorGradient(cg2); console.log(cg2); } })(cg)}); this.presetList [newIndex] = cg; } @bobince:当然可以。 上述代码段是 PresetManager.js 的一部分,而listController是 ListWrapper.js 类的实例 http:// code.assembla.com/kpg/subversion/nodes/GradientLab/lib-js/PresetManager.js http://code.assembla.com/kpg/subversion/nodes/GradientLab/lib-js/ListWrapper.js @Matt: cg是ColorGradient的一个实例。一个自定义类。此外,确保总是有效值作为cg传递。 (当你有几分钟的时候,你可以下载整个assembla仓库为ZIP压缩文件,解压并测试FF> 3.5并启用Firebug控制台。)解决方案回答可以在这个问题中找到: 'JavaScript支持使用局部变量的闭包? Hey guys, I am stuck at the following code. At first I'll describe the use-case: The function "addPreset" gets called with an instance of ColorGradient. When calling this.listController.addItem(...) a callback function named onSelect ist supplied, which gets called everytime the onSelect-event on the listController-item is triggered. What I wanted to do is wrapping the call to GLab.ColorSlider.applyColorGradient(...) into a new closure, so that the assigned value of addPreset's "cg" argument"* will be "caught" inside it. But it doesn't work. PROBLEM: Now everytime addPreset is called, the value of cg (being passed with a call) will override all values that bad been assigned before. However, this.presetList holds always correct values (the ones I expected to be caught inside the closure-function. Even inserting an anonymous function for breaking the scope doesn't help.Please help me. :-) Thanks, so far function addPreset(cg) { if (!(cg instanceof ColorGradient)) { throw new TypeError("PresetManager: Cannot add preset; invalid arguments received"); } var newIndex = this.listController.addItem(cg.getName(), { onSelect: (function(cg2) { return function() { // addPreset's scope should now be broken GLab.ColorSlider.applyColorGradient(cg2); console.log(cg2); } })(cg) }); this.presetList[newIndex] = cg;}@bobince: of course you can.the code snippet above is part of PresetManager.js and the listController is an instance of the class ListWrapper.jshttp://code.assembla.com/kpg/subversion/nodes/GradientLab/lib-js/PresetManager.jshttp://code.assembla.com/kpg/subversion/nodes/GradientLab/lib-js/ListWrapper.js@Matt: cg is an instance of ColorGradient. A custom class of myself. Further more, it is assured, that always "valid" values are passed in as cg. (When you'd have a few minutes you can download the whole assembla repo as zip-archive. Unzip and test in FF > 3.5 with Firebug console enabled.) 解决方案 Answer can be found in this question: Doesn't JavaScript support closures with local variables? 这篇关于JavaScript关闭的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 09:22