closures

How to call functions that are nested inside a JQuery Plugin?

社会主义新天地 提交于 2019-12-04 07:40:01
问题 My goal is to be able to call functions that are inside my JQuery plugin. What is the correct syntax? For example, this does not work: <a href="#" id="click_me">Click Me</a> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script> (function($) { $.fn.foo = function(options) { do_stuff = function(){ console.log("hello world!"); // works do_other_stuff = function(){ alert("who are you?"); } } // function } // function })(jQuery); $("body").foo(); $("

PHP - Difference between 'use()' or 'global' to access a global variable in a closure?

99封情书 提交于 2019-12-04 07:37:06
Is there any kind of performance or other difference between following two cases of accessing a global variable in a closure: Case 1: $closure = function() use ($global_variable) { // Use $global_variable to do something. } Case 2: $closure = function() { global $global_variable; // Use $global_variable to do something. } There is an important difference between your two examples: $global_variable = 1; $closure = function() use ($global_variable) { return $global_variable; }; $closure2 = function() { global $global_variable; return $global_variable; }; $global_variable = 99; echo $closure(); /

Are completion handler closures always running in the background thread?

谁说我不能喝 提交于 2019-12-04 07:36:04
Completion handler closures are common in iOS development, such as dataTask(with:completionHandler:) in the URLSession class. The UI engine is managed by the main thread, the API calls by URLSession are run under the background thread and must be dispatched to the main thread if a UI update is needed in the handler. Question 1 : Do all completion handler closures from the iOS framework run in the background thread? Question 1.1 : Do all escaping closures, for example, created by developers, run in the background thread? Question2 : I've seen up to 8 threads in the iPhone X simulator. Which one

Closures in PHP… what, precisely, are they and when would you need to use them?

北城余情 提交于 2019-12-04 07:26:10
问题 So I'm programming along in a nice, up to date, object oriented fashion. I regularly make use of the various aspects of OOP that PHP implements but I am wondering when might I need to use closures. Any experts out there that can shed some light on when it would be useful to implement closures? 回答1: PHP will support closures natively in 5.3. A closure is good when you want a local function that's only used for some small, specific purpose. The RFC for closures give a good example: function

Javascript closures vs PHP closures, what's the difference?

懵懂的女人 提交于 2019-12-04 07:25:41
问题 What are the differences between closures in JS and closures in PHP? Do they pretty much work the same way? Are there any caveats to be aware of when writing closures in PHP? 回答1: One difference is how both cope with storing the context in which an anonymous function is executed: // JavaScript: var a = 1; var f = function() { console.log(a); }; a = 2; f(); // will echo 2; // PHP $a = 1; $f = function() { echo $a; }; $a = 2; $f(); // will result in a "PHP Notice: Undefined variable: a in

Definition of 'closures'

孤街醉人 提交于 2019-12-04 06:56:04
Let me ask one question. It's about closures in JavaScript, but not about how they work. David Flanagan in his "JavaScript The Definitive Guide 6th Edition" wrote: ... Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them. ... Is this correct? Can I call every function (function object + it's scope) a "closure"? And stacks' tag "closures" says: A closure is a first-class function that refers to (closes over) variables from the scope in which it was defined. If the closure still exists after its defining scope ends, the variables

Why are closures better than global variables for preserving variables?

情到浓时终转凉″ 提交于 2019-12-04 06:52:25
I understand how closures work within JavaScript, but my question is why would you go through all the trouble of making a closure to preserve a variable? Couldn't you just make the variable global? Or would that clutter up the global scope and make your code be prone to errors. It's a scoping issue. Global variables are just that: Global, to everyone . With closures, the scope (visibility) of the variables can be better controlled, which means the possible unintended side effects can be better controlled. http://en.wikipedia.org/wiki/Global_variable [Globals] are usually considered bad

Closures as solution to data merging idiom

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 06:49:30
I'm trying to wrap my head around closures, and I think I've found a case where they might be helpful. I have the following pieces to work with: A set of regular expressions designed to clean state names, housed in a function A data.frame with state names (of the standardized form that the function above creates) and state ID codes, to link the two (the "merge map") The idea is, given some data.frame with sloppy state names (is the capital listed as "Washington, D.C.", "washington DC", "District of Columbia", etc.?), to have a single function return the same data.frame with the state name

JavaScript Event implementation to Closure based Object

我怕爱的太早我们不能终老 提交于 2019-12-04 06:41:22
问题 I have a Object based on some closure, and want to implement event scheme here: var class1 = function(val1) { var val = val1; //------ want to call a method of Object of class1-------- var self = this; setTimeout(function() { self.onEvent(); }, 1000); //---------------- return { f1: function() { return val; }, onEvent: function() { console.log('not implemented yet. Override'); } }; }; var obj1 = class1(5); console.log(obj1.f1()); //5 obj1.onEvent(); //not implemented yet. Override obj1

How do I handle async requests in Swift?

我的梦境 提交于 2019-12-04 06:25:51
问题 I make a web service call in one of my functions. I'm having trouble how to structure it so the logic is clean and not redundant. Here's what I have: public func getTimes() -> [TimeName: MyResult] { let deferredTask: () -> [TimeName: MyResult] = { var computed = self.computeTimes() // Do something return computed } // Calculate timezone if let tz = timezone { timeZone = tz return deferredTask() } else { NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { (data, response, error