closures

What does a block of code in parentheses mean in JavaScript/jQuery? [duplicate]

一世执手 提交于 2019-12-03 16:14:23
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What does (function($) {})(jQuery); mean? I've seen a lot of jQuery code with the following sort of syntax, but I don't really understand what it means. It shows up in this answer and this answer on a question about code organization. Both talk about namespacing, so I'm guessing that's what it accomplishes. var foo = (function () { var someVar; function someFunc() { return true; } })(); Is this for namespacing,

Closure/scope JavaScript/jQuery

狂风中的少年 提交于 2019-12-03 15:29:54
I'm trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I'm not quite getting it to work. First, all the JS works outside my anonymous function, but once I put it in the anonymous function I get an error of "crossfade is not defined". Does anyone see anything completely obvious that I am missing? I'm not quite getting why the the setInterval/crossfade works outside the anonymous function but not inside. Anything inside start() should be able to see vars/functions outside start() and it should all be protected in the closure created

Wait for completion handler to finish - Swift

馋奶兔 提交于 2019-12-03 15:25:15
I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function checkAvailability which checks multiple things, including the UserNotification authorization status. func checkAvailabilty() -> Bool { // // other checking // var isNotificationsEnabled = false UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in if granted { isNotificationsEnabled = true } else { isNotificationsEnabled = false } }) } if isNotificationsEnabled { return true } else { // Throw alert: Remind user to

Differrence between closure and function as argument in swift

試著忘記壹切 提交于 2019-12-03 15:02:42
问题 I have almost 4 years of experience with Objective C and a newbie in swift. Am trying to understand the concept of swift from the perspective of Objective C. So if I am wrong please guide me through :) In objective c, we have blocks (chunck of code which can be executed later asynchronously) which made absolutely perfect sense. But in swift now we can pass a function as a parameter to another function, which can be executed later, and then we have closure as well. As per Apple "functions are

Swift use selector argument like a closure

杀马特。学长 韩版系。学妹 提交于 2019-12-03 14:56:56
问题 I was just wondering if it was possible to pass a function to a button action (which is usually a selector). For example, normally I'd say: UIBarButtonItem(title: "Press", style: .Done, target: self, action: "functionToCall") func functionToCall() { // Do something } But I was wondering if it's possible to do something like: UIBarButtonItem(title: "Press", style: .Done, target: self, action: { // Do Something }) Reason I want to do this is because my function is super simple and it seems like

What's wrong with this style of coding JavaScript? (closures vs. prototypes)

梦想与她 提交于 2019-12-03 14:41:47
We have been debating how best to handle objects in our JS app, studying Stoyan Stefanov's book, reading endless SO posts on 'new', 'this', 'prototype', closures etc. (The fact that there are so many, and they have so many competing theories, suggests there is no completely obvious answer). So let's assume the we don't care about private data . We are content to trust users and developers not to mess around in objects outside the ways we define. Given this , what (other than it seeming to defy decades of OO style and history) would be wrong with this technique? // namespace to isolate all

Automapper + EF4 + ASP.NET MVC - getting 'context disposed' error (I know why, but how to fix it?)

扶醉桌前 提交于 2019-12-03 14:12:24
I have this really basic code in a MVC controller action. It maps an Operation model class to a very basic OperationVM view-model class . public class OperationVM: Operation { public CategoryVM CategoryVM { get; set; } } I need to load the complete list of categories in order to create a CategoryVM instance. Here's how I (try to) create a List<OperationVM> to show in the view. public class OperationsController : Controller { private SomeContext context = new SomeContext (); public ViewResult Index() { var ops = context.Operations.Include("blah...").ToList(); Mapper.CreateMap<Operation,

Go closure variable scope

笑着哭i 提交于 2019-12-03 14:00:05
I'm reading 'CreateSpace An Introduction to Programming in Go 2012' and on page 86 I found this evil magic func makeEvenGenerator() func() uint { i := uint(0) return func() (ret uint) { ret = i i += 2 return } } // here's how it's called nextEven := makeEvenGenerator() fmt.Println(nextEven()) fmt.Println(nextEven()) fmt.Println(nextEven()) 1) Why is i not resetting ? 2) is nextEven() returning and uint or is Println so smart that it can work with everything ? For the sake of clarity, I'll assign names to both functions: func makeEvenGenerator() func() uint { // call this "the factory" i :=

Where does Python store the name binding of function closure?

天涯浪子 提交于 2019-12-03 13:26:52
So recently I understand the concept of function closure. def outer(): somevar = [] assert "somevar" in locals() and not "somevar" in globals() def inner(): assert "somevar" in locals() and not "somevar" in globals() somevar.append(5) return somevar return inner function = outer() somevar_returned = function() assert id(somevar_returned) == id(function.func_closure[0].cell_contents) As much as I understand, the objective of function closure is to keep an active reference to the object, in order to avoid garbage collection of this object. This is why the following works fine : del outer somevar

Swift closure as values in Dictionary

你说的曾经没有我的故事 提交于 2019-12-03 13:09:45
I'm trying to use an Objective-C library which expects a NSDictionary as its return type. Within the NSDictionary , I can return values of any type, including blocks. I cannot figure out if there is a way to write an analogous swift method that returns a Dictionary with a closure or a string as a possible value type. I can't use AnyObject as the value type for the dictionary so this doesn't work: Dictionary<String,AnyObject> = ["Key":{(value:AnyObject) -> String in return value.description] I get a Does not conform to protocol error from the compiler regarding the closure and AnyObject . Is