What underlies this JavaScript idiom: var self = this?

I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo: function Note() { var self = this; var note = document.createElement(‘div’); note.className=”note”; note.addEventListener(‘mousedown’, function(e) { return self.onMouseDown(e) }, false); note.addEventListener(‘click’, function() { return self.onNoteClick() }, false); this.note = note; // … } The author uses self in some places (the … Read more

In PHP, what is a closure and why does it use the “use” identifier?

I’m checking out some PHP 5.3.0 features and ran across some code on the site that looks quite funny: public function getTotal($tax) { $total = 0.00; $callback = /* This line here: */ function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . “::PRICE_” . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + … Read more

What is the purpose of a self executing function in javascript?

In javascript, when would you want to use this: (function(){ //Bunch of code… })(); over this: //Bunch of code… 20 s 20 It’s all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of … Read more

What is the difference between a ‘closure’ and a ‘lambda’?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we’re here, how do they differ from a regular function? 15 s 15 There is a lot of confusion around lambdas and closures, even in the answers to this StackOverflow question … Read more