Mercurial > js4py
view core-javascript/bind-is-transient.rst @ 213:0ea90a1819c2 default tip
Custom theming
| author | Radomir Dopieralski <radomir.dopieralski@allegro.pl> |
|---|---|
| date | Wed, 27 Jun 2012 16:35:54 +0200 |
| parents | abfb176c7346 |
| children |
line wrap: on
line source
Bind is transient ================= Test code --------- Here's the test code for attribute bind. .. literalinclude:: /core-javascript-work/tutorial.js :language: javascript :start-after: start transient-bind :end-before: end transient-bind Discussion ---------- We create an object that has a *return_this* method. The outcome of calling *return_this* depends on how it is called. There's a logic in the test code that produces contradictory, or at least surprising, outcomes. From .. code-block: javascript obj.return_this === return_this; method_deferred === obj.return_this; method_deferred === return_this; we would expect .. code-block: javascript obj.return_this() === return_this(); method_deferred() === obj.return_this(); method_deferred() === return_this(); But this means that in *obj.return_this()* the *obj* is not relevant. But JavaScript (or its designers) wanted objects to have methods, so they introduced a special rule. Explanation ----------- JavaScript does not distinguish classes and instances. Python, and perhaps other dynamic languages, does. This allows Python to supply a bound method, which retains a reference to the object, when a function belonging to the class is called on an instance. Here's a Python (version 3) command line dialogue that illustrates this. >>> def method(): pass # The function. ... >>> class A: method = method # The class. >>> A.method # Function from class. <function method at 0x15be518> >>> A.method is method # Same as the original function. True >>> a = A() # Instance. >>> a.method # Bound method, not original function. <bound method A.method of <__main__.A object at 0x16b9d10>> The odd behaviour shown in the JavaScript test code is a consequence of JavaScript not distinguishing between classes and instances.
