closures

Is it possible to simulate constants in Javascript using closures?

不打扰是莪最后的温柔 提交于 2019-12-31 06:05:26
问题 Is it possible to simulate constants in Javascript using closures? If so, can you please furnish me with an example? 回答1: Firefox and Chrome both support the const keyword. IE does not. So if you need constants and don't need to support IE, const isn't a bad choice. Keep in mind though, neither browser produces a runtime error when a value is assigned to a const ; the values merely remains unchanged. Otherwise, you must use functions to define constants that cannot be modified: function PI()

Is it possible to simulate constants in Javascript using closures?

China☆狼群 提交于 2019-12-31 06:04:38
问题 Is it possible to simulate constants in Javascript using closures? If so, can you please furnish me with an example? 回答1: Firefox and Chrome both support the const keyword. IE does not. So if you need constants and don't need to support IE, const isn't a bad choice. Keep in mind though, neither browser produces a runtime error when a value is assigned to a const ; the values merely remains unchanged. Otherwise, you must use functions to define constants that cannot be modified: function PI()

Scoping problem with Javascript callback

巧了我就是萌 提交于 2019-12-31 04:37:18
问题 I am having some trouble getting a callback function to work. Here is my code: SomeObject.prototype.refreshData = function() { var read_obj = new SomeAjaxCall("read_some_data", { }, this.readSuccess, this.readFail); } SomeObject.prototype.readSuccess = function(response) { this.data = response; this.someList = []; for (var i = 0; i < this.data.length; i++) { var systemData = this.data[i]; var system = new SomeSystem(systemData); this.someList.push(system); } this.refreshList(); } Basically

Use of 'this' in closure

时光毁灭记忆、已成空白 提交于 2019-12-31 03:20:30
问题 I'm just curious... how am I supposed to use 'this' within a jQuery function? For example, if I have some code like this... headEl.find("form.blog-search input").focus(function() { $(this).next("span").animate({opacity:1}, 200); }) It works fine, however when linting I get the warning of " Use of ' this ' in closure ". Is this something I should just ignore, or is there something I can do to not only solve the warning, but improve my code? Update: Based on Kevin B's comment below, I changed

need more explanation on w3schools javascript closure example

故事扮演 提交于 2019-12-31 02:54:11
问题 I'm trying to understand closures and am looking at the W3Schools javascript tutorial. This is one example they give by making a counter. <body> <p>Counting with a local variable.</p> <button type="button" onclick="myFunction()">Count!</button> <p id="demo">0</p> <script> var add = (function () { var counter = 0; return function () {return counter += 1;} })(); function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> Example Explained The variable add is

How do I access variables that are inside closures in Swift?

江枫思渺然 提交于 2019-12-31 00:45:06
问题 I'm new to Swift and I'm trying to get the result from this function. I don't know how to access variables that are inside the closure that is passed to the sendAsynchronousRequest function from outside the closure. I have read the chapter on closures in the Apple Swift guide and I didn't find an answer and I didn't find one on StackOverflow that helped. I can't assign the value of the 'json' variable to the 'dict' variable and have that stick outside of the closure. var dict: NSDictionary!

Repetitive try-catch blocks with Groovy 'with' closure?

半世苍凉 提交于 2019-12-30 20:39:20
问题 I have the following Groovy class: @Slf4j class WidgetService { WidgetDao widgetDao = new WidgetDao() createWidget(String name, int type) { try { widgetDao.createWidget(name, type) } catch(WidgetException wexc) { log.error(wexc) int x = doFizz() long y = doBuzz(x) determineHowToHandle(y) } } Widget getWidgetById(Long id) { try { widgetDao.getWidgetById(id) } catch(WidgetException wexc) { log.error(wexc) int x = doFizz() long y = doBuzz(x) determineHowToHandle(y) } } Widget getWidgetByName

JavaScript closure and the this object

走远了吗. 提交于 2019-12-30 19:53:13
问题 I thought I had a reasonable understanding of the this object in JavaScript. When dealing with objects, callbacks, and both events and handlers, I haven't had a problem with it since time immemorial. Now, however, all has changed. I've fallen head over heels in love with JavaScript. Pure JS, that is, not jQuery, prototype.js, dojo... So naturally, I've taken to using closures. In some cases, though, this is catching me off guard here. Take this snippet for one: function anyFunc(par) { /

Avoid “Use of unassigned local variable” error

怎甘沉沦 提交于 2019-12-30 10:08:20
问题 I have a two methods that are equivalent to this (pardon the contrived example): public void WithResource(Action<Resource> action) { using (var resource = GetResource()) { action(resource); } } public void Test() { int id; SomeObject someObject; WithResource((resource) => { id = 1; someObject = SomeClass.SomeStaticMethod(resource); }); Assert.IsNotNull(someObject); Assert.AreEqual(id, someObject.Id); } (There's some more logic in the WithResource call I'm trying to factor out.) I'm getting

Avoid “Use of unassigned local variable” error

99封情书 提交于 2019-12-30 10:07:09
问题 I have a two methods that are equivalent to this (pardon the contrived example): public void WithResource(Action<Resource> action) { using (var resource = GetResource()) { action(resource); } } public void Test() { int id; SomeObject someObject; WithResource((resource) => { id = 1; someObject = SomeClass.SomeStaticMethod(resource); }); Assert.IsNotNull(someObject); Assert.AreEqual(id, someObject.Id); } (There's some more logic in the WithResource call I'm trying to factor out.) I'm getting