closures

Self destructing Javascript function - How does it work?

邮差的信 提交于 2019-12-20 10:10:40
问题 So I found this piece of code and it obviously works (as it has been in production for years): window[someMethod] = function (tmp) { callback({prop:"val"}, tmp); // Garbage collect window[someMethod] = undefined; try { delete window[someMethod]; } catch (e) { } if (head) { head.removeChild(script); } // head refers to DOM head elem and script refers to some script file elem }; Curious to know, how does it work? How can it set itself to undefined within its body and try to delete itself? Does

'[weak self]' in RXSwift closures

巧了我就是萌 提交于 2019-12-20 09:50:40
问题 Do i need to use [weak self] within RXSwift subscribeNext closures? I have the code: searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance).subscribeNext { searchText in self.viewModel.searchForLocation(searchText) }.addDisposableTo(DisposelBag.sharedDisposelBag.disposeBag) Do i need to modify it so that there is a [weak self] capture list at the beginning of the closure? Like this: searchController.searchBar.rx_text.throttle(0.2, scheduler: MainScheduler.instance

extjs - how correctly call a controller method from another controller or closure

喜欢而已 提交于 2019-12-20 09:32:03
问题 I'm new to extjs and I'm using the MVC architecture. When my application references a method of a controller, I do it that way (in MyApp.Application ): Mb.app.getController('Main').myMethod(); It is already long, but I think this is the way to do. When a controller calls it's own method in a closure, I was led to use this code (in MyApp.controller.Main : controllerMethodOne: function(){ Ext.Ajax.request({ url: ..., params: ..., success: (function(response){ list = Ext.JSON.decode(response

Why are there memory allocations when calling a func

笑着哭i 提交于 2019-12-20 08:56:10
问题 I have the following program which construct a local Func from two static methods. But strangely, when I profile the program, it allocated close to a million Func objects. Why invoking Func object is also creating Func instances? public static class Utils { public static bool ComparerFunc(long thisTicks, long thatTicks) { return thisTicks < thatTicks; } public static int Foo(Guid[] guids, Func<long, long, bool> comparerFunc) { bool a = comparerFunc(1, 2); return 0; } } class Program { static

How do I create a memory leak in JavaScript?

人走茶凉 提交于 2019-12-20 08:40:21
问题 I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory consumption shown in the Activity Monitor does not really increase. Is something wrong with my script or is this no longer an issue with modern browsers? <html> <body> </body> <script> var i, el; function attachAlert(element) { element.onclick = function() { alert(element.innerHTML); }; } for (i = 0; i < 1000000; i++) {

Is it necessary to use [unowned self] in closures of UIView.animateWithDuration(…)?

ε祈祈猫儿з 提交于 2019-12-20 08:38:15
问题 UIView.animateWithDuration(1, animations: { [unowned self] in self.box.center = self.boxTopRightPosition }, completion: { [unowned self] completed in self.box.hidden = true }) Is it necessary to avoid memory leak? 回答1: No, it is not needed in this case. animations and completion are not retained by self so there is no risk of strong retain cycle. 回答2: Well, "necessary" isn't the same as "recommended". If your question is if it's necessary then @Kirsteins' response is fine, however imagine the

Making counter using closure and self-invoking function

巧了我就是萌 提交于 2019-12-20 07:10:04
问题 I'm wondering why this code doesn't work, var uniqueInteger = function() { var counter = 0; return function() { return counter++; } }; console.log(uniqueInteger()()); // 0 console.log(uniqueInteger()()); // 0 console.log(uniqueInteger()()); // 0 console.log(uniqueInteger()()); // 0 and this code does. The only difference is making the function self invoked instead of invoking it in console.log() var uniqueInteger = (function() { var counter = 0; return function() { return counter++; } }());

Array of promises [duplicate]

天涯浪子 提交于 2019-12-20 06:04:57
问题 This question already has answers here : Promise.all().then() resolve? (2 answers) Closed 2 years ago . I am working on a piece where I have to get json data from an API for different cities and build DOM. So far, I have been able to do both. Only problem is that the API response time for the different cities are different. So, when I build the DOM they are not in the same order as I call the function. From what I recall I need to use promise to get it that order. My questions now are: How

JavaScript Closures example and explanation

感情迁移 提交于 2019-12-20 03:29:07
问题 I have found this example of a closure on codeproject but it does not explain how it works. function getMultiplier(multiplyBy){ function multiply(num){ return multiplyBy * num; } return multiply; } var multiplyByTwo = getMultiplier(2); var multiplyByTen = getMultiplier(10); var twoIntoFive = multiplyByTwo(5); var tenIntoSix = multiplyByTen(6); console.log(twoIntoFive); // 10 console.log(tenIntoSix); // 60 Now i am going to assume, with my C brain what is happening. Please correct me or give

The parameter type `T` may not live long enough

别说谁变了你拦得住时间么 提交于 2019-12-20 03:17:46
问题 I'm trying to write a small program in Rust but I can't get it work. I have reproduced the error in a smaller script: fn main() { let name = String::from("World"); let test = simple(name); println!("Hello {}!", test()) } fn simple<T>(a: T) -> Box<Fn() -> T> { Box::new(move || -> T { a }) } When I compile it, I get this error: error[E0310]: the parameter type `T` may not live long enough --> test.rs:8:9 | 7 | fn simple<T>(a: T) -> Box<Fn() -> T> { | - help: consider adding an explicit lifetime