closures

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

一笑奈何 提交于 2019-12-02 15:42:54
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? Stefan Gehrig 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 Untitled.php on line 5" To fix this notice you'll have to use the use syntax: $a = 1; $f = function

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

牧云@^-^@ 提交于 2019-12-02 15:18:11
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(); $("#click_me").click(function(){ $.fn.foo.do_stuff.do_other_stuff(); // doesn't work }); </script> when you

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

老子叫甜甜 提交于 2019-12-02 15:09:43
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? 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. Well, "necessary" isn't the same as "recommended". If your question is if it's necessary then @Kirsteins' response is fine, however imagine the situation where you want to animate something in your view controller after some work, but your view controller

Broken closure - please help me fix it

别等时光非礼了梦想. 提交于 2019-12-02 14:42:59
问题 in a related question I have posted this code. It almost works, but the counter doesn't. Can we fix it? (no jQuery, please) <script type="text/javascript"> var intervals = []; var counters = { "shoes":{"id":"shoe1","minutes":1,"seconds":5}, "trousers":{"id":"trouser1","minutes":10,"seconds":0} }; // generate this on the server and note there is no comma after the last item window.onload = function() { for (var el in counters) { countdown(counters[el]) }; } function countdown(element) {

jQuery click event firing immediately

為{幸葍}努か 提交于 2019-12-02 14:39:04
问题 I have the following code in the web app I'm developing: var bgcolor = ''; var row = 0; for(var i = 0; i < result.data.length; i++) { // Set alternating background colors bgcolor = ( i%2 == 0) ? ' style=\'background-color: white;\'': ' style=\'background-color: whitesmoke;\''; $( "div.row" ).append("\ <div class='search_container'"+bgcolor+">\ <div class='search_result_client_heading'>"+result.data[i]['client_name']+"</div>\ <div class='search_result_industry_heading'>"+result.data[i][

Is there any particular use of closure in swift? and what's the benefit?

[亡魂溺海] 提交于 2019-12-02 14:11:18
I have learnt Swift for a while and I have read the Swift language guide. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94 The concept closure is new to me. I think I can understand how to use it, but where I can use it? what is the benefit of it? I googled and get the answer When to use closures in swift? I do not thinks the answer is satisfying. The language guide writes so much about it, I guess it is a very important feature of the language, and maybe it's widely used in the

Why do we need fibers

风格不统一 提交于 2019-12-02 13:49:51
For Fibers we have got classic example: generating of Fibonacci numbers fib = Fiber.new do x, y = 0, 1 loop do Fiber.yield y x,y = y,x+y end end Why do we need Fibers here? I can rewrite this with just the same Proc (closure, actually) def clsr x, y = 0, 1 Proc.new do x, y = y, x + y x end end So 10.times { puts fib.resume } and prc = clsr 10.times { puts prc.call } will return just the same result. So what are the advantages of fibers. What kind of stuff I can write with Fibers I can't do with lambdas and other cool Ruby features? Fibers are something you will probably never use directly in

How to use my own version of jQuery with browserified modules

这一生的挚爱 提交于 2019-12-02 13:48:23
问题 (I should clarify up front: My question is about closures and client module patterns in Javascript. It's not about how to use jQuery.noConflict().) I've got a bit of Javascript that people can add to their websites. I want my own code to have access to a $ variable which resolves to a specific version of jQuery that's independent of whatever the page has loaded. This is easy if all my code is in a single file, inside a closure that I define. But I'm struggling to find a clean way to do this

Can someone explain this Javascript closure example

北城余情 提交于 2019-12-02 13:31:33
function makeAdder(x) { return function(y) { console.log("X:" + x + " Y:" + y); return x + y; }; } var add5 = makeAdder(5); var add10 = makeAdder(10); console.log(add5(2)); console.log(add10(2)); Ok, I am a bit confused with this example on developer.mozilla under closures. If someone can explain with as much detail as possible in order for me to get my head around closures. Ignore the console.log, I just added that to see what values are displayed and from that I can see that x is 5 and y is 2 when you execute add5 for example. I consoled add5() to see what I get and I got NaN - and I am

closure in for-loop > different tries failed

我与影子孤独终老i 提交于 2019-12-02 13:05:10
I want to create a 2-dimensional array with an index-number in each first element. EDIT: thx a lot so far.. @carl: I did so much function creation just to show the kind of tries I did.. jonhopkins idea gave rise to this: this works: $('#create_indexed_array').click(function() { var new_array = [[9,9],[9,9],[9,9],[9,9],[9,9]]; for (var i = 0; i < 5; i++) { new_array[i][0] = i; } alert(JSON.stringify(new_array)); }); BUT this works not: $('#create_indexed_array').click(function() { var new_array = new Array(new Array()); for (var i = 0; i < 2; i++) { new_array[0][i] = ""; // create cols } for