This link has been bookmarked by 100 people . It was first bookmarked on 02 Jul 2008, by Michael C. Harris.
-
22 Jun 17
-
15 Oct 12
-
08 Oct 12
-
29 Sep 12
-
09 Sep 12
Fahim FarookSo how do we fix it? We bind explicitly—that is, we explicitly state to what this will point to within the method when it gets called. And how do we do that? JavaScript provides us with two options: apply and call.
-
31 Aug 12
-
02 Jun 12
-
16 May 12
-
07 Apr 12
-
08 Feb 12
-
16 Jan 12
-
06 Nov 11
-
27 Sep 11
amit_eing—something I’ll refer to as “binding loss.” It happens whenever you’re accessing a method through a reference instead of directly through its owner object. The method loses its implicit binding, and this stops referencing its owner object and goes back to its default value, which in this case is window (so if window had a name property by th
-
17 Sep 11
-
14 Sep 11
-
12 Sep 11
-
07 Apr 11
-
25 Mar 11
-
25 Nov 10
Viktor SzűcsNagyon jól elmagyarázza az egyik legfontosabb és nehezen felismerhető problémát javascriptben, amire egyszerűen csak "binding loss"-ként hivatkozik. Aki javascriptben dolgozik, ill. valamilyen js library-t használ, mindenképp olvassa el ezt a cikket, főleg, ha mélységeiben szeretné megismerni a problémát és nem elégszik meg a felületes magyarázatokkal.
-
24 Sep 10
-
29 Apr 10
Marg WilkinsonMost developers don’t know about—or don’t care enough about—binding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions
-
01 Mar 10
-
21 Oct 09
Ethan GardnerMost developers dont know aboutor dont care enough aboutbinding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions on most JavaScript-related support channels, and thousandsif not millionsof hairs being tortured aw
-
08 Jun 09
-
06 Apr 09
-
15 Mar 09
-
04 Mar 09
Attila GyörffyMost developers don’t know about—or don’t care enough about—binding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions on most JavaScript-related support channels, and thousands—if not millions—of hairs being tortu
-
28 Oct 08
-
29 Sep 08
-
19 Aug 08
-
04 Aug 08
-
31 Jul 08
-
22 Jul 08
-
17 Jul 08
danOH, at last, someone enumerating "this" in a way that doesn't boil don to 'just check it in firebug'
-
15 Jul 08
-
13 Jul 08
-
12 Jul 08
-
10 Jul 08
-
08 Jul 08
-
07 Jul 08
-
06 Jul 08
-
05 Jul 08
-
04 Jul 08
-
Kenneth PriisholmMost developers don’t know about—or don’t care enough about—binding in JavaScript...
-
03 Jul 08
-
Oncle TomExplication sur les passages de paramètres par référence en JavaScript (binding). Ou comment utiliser apply et call pour éviter de se perdre dans des "this" invalides.
javascript binding oop scope alistapart development code howto clevermarks for:meatshake
-
Tim Lossen"Most developers don’t know about—or don’t care enough about—binding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions on most JavaScript-related support channels ..."
-
02 Jul 08
-
Frederik Van ZandeMost developers don’t know about—or don’t care enough about—binding in JavaScript. Yet this single issue is responsible for a sizeable portion of the questions on most JavaScript-related support channels, and thousands—if not millions—of hairs being tortured away from developer heads every single day. Yet with a little attention to this oft-overlooked subject, you can avoid wasting your time, energy, and patience and move on to more powerful, efficient scripting.
-
-
In short, for most OOP languages, binding is implicit. This is true in Java, C#, Ruby, Delphi, and C++
-
PHP and JavaScript do require you to explicitly state which object you’re accessing, even if it’s the current one
-
This is the single most important issue with JavaScript binding—something I’ll refer to as “binding loss.” It happens whenever you’re accessing a method through a reference instead of directly through its owner object
-
The method loses its implicit binding, and
thisstops referencing its owner object and goes back to its default value, which in this case iswindow -
Binding-sensitive code patterns
-
Every JavaScript function is equipped with an
applymethod that allows you to call that function with specific binding (a specificthis, if you will). It takes two arguments: the binding object, and an array of the arguments to be passed to the function -
When you do know exactly which arguments you want to pass,
callmay feel nicer, as it takes the arguments themselves, not an array of them -
The only way to achieve this requires us to wrap our original method in another one, that will perform the
applycall -
- That function, when called, will take our original method and invoke
- the original object’s binding (the variable named
object), and - whatever arguments were provided at call time, as an array
applyon it, passing: - the original object’s binding (the variable named
-
JavaScript frameworks do it
-
var that = this
-
that.
-
This code uses a language feature called “lexical closure.” In short, closures let code at point A access identifiers declared in scopes surrounding A
-
- Any member access must be qualified with the object it pertains to, even when it is
this. - Any sort of function reference (assigning as a value, passing as an argument) loses the function’s original binding.
- JavaScript provides two equivalent ways of explicitly specifying a function’s binding when calling it:
applyandcall. - Creating a “bound method reference” requires an anonymous wrapper function, and a calling cost. In specific situations, leveraging closures may be a better alternative.
- Any member access must be qualified with the object it pertains to, even when it is
-
-
Michael C. Harris# Any member access must be qualified with the object it pertains to, even when it is this.
# Any sort of function reference (assigning as a value, passing as an argument) loses the function’s original binding.
# JavaScript provides two equivalent ways of explicitly specifying a function’s binding when calling it: apply and call.
# Creating a “bound method reference” requires an anonymous wrapper function, and a calling cost. In specific situations, leveraging closures may be a better alternative.
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.