- Early binding
- Late binding
- Binding method that does not exist
- Total
The usual bind method is called “early binding,” since it captures the binding immediately.
Once the values are bound, they can no longer be changed. In particular, if the method of the object that is tied, someone will override - the “tied” function will not notice.
Late binding is more flexible; it allows you to override the bound method at any time.
Early binding
For example, let's try to override the method for early binding:
01 |
function bind(func, context) { |
03 |
return func.apply(context, arguments); |
08 |
sayHi: function () { alert( 'Привет!' ); } ); } |
11 |
// привязали метод к объекту |
12 |
var userSayHi = bind(user.sayHi, user); |
14 |
// понадобилось переопределить метод |
15 |
user.sayHi = function () { alert( 'Новый метод!' ); } ); } |
17 |
// будет вызван старый метод, а хотелось бы - новый! |
18 |
userSayHi(); // выведет "Привет!" |
... Binding is still working with the old method, despite being redefined.
Late binding
When late binding, bind will call not the function that was in sayHi at the time of binding, but the function that was at the time of the call. **
There is no built-in method for this, so you need to implement it.
The syntax is:
var func = bindLate(obj, "method" ); |
obj
An object
method
Method Name (String)
Code:
1 |
function bindLate(context, funcName) { |
3 |
return context[funcName].apply(context, arguments); |
This call is similar to the usual bind , one of the variants of which just looks like bind(obj, "method") , but it works differently.
The search for a method in the object: context[funcName] , is performed when invoked, by the wrapper itself .
Therefore, if the method is redefined - the last option will always be used.
In particular, the example discussed above will work correctly:
01 |
function bindLate(context, funcName) { |
03 |
return context[funcName].apply(context, arguments); |
08 |
sayHi: function () { alert( 'Привет!' ); } ); } |
11 |
var userSayHi = bindLate(user, 'sayHi' ); |
13 |
user.sayHi = function () { alert( 'Здравствуйте!' ); } ); } |
15 |
userSayHi(); // Здравствуйте! |
Binding method that does not exist
Later binding allows you to bind to an object even a method that is not there yet!
Of course, it is assumed that by the time of the call it will already be defined
For example:
01 |
function bindLate(context, funcName) { |
03 |
return context[funcName].apply(context, arguments); |
10 |
// ..а привязка возможна! |
11 |
var userSayHi = bindLate(user, 'sayHi' ); |
13 |
// по ходу выполнения добавили метод.. |
14 |
user.sayHi = function () { alert( 'Привет!' ); } ); } |
16 |
userSayHi(); // Метод работает: Привет! |
In a sense, late binding is always better than early. It is more convenient and reliable, because it always calls the desired method, which is in the object now.
But it also incurs a small overhead - searching for a method with each call.
Total
Late binding looks for a function in the object at the time of the call.
It is used for binding in cases where the method can be redefined after binding or does not exist at the time of binding .
Wrap for late binding (without currying):
1 |
function bindLate(context, funcName) { |
3 |
return context[funcName].apply(context, arguments); |
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone