You get a bonus - 1 coin for daily activity. Now you have 1 coin

Late bindLate binding

Lecture



  1. Early binding
  2. Late binding
  3. Binding method that does not exist
  4. 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) {
02 return function () {
03 return func.apply(context, arguments);
04 };
05 }
06
07 var user = {
08 sayHi: function () { alert( 'Привет!' ); } ); }
09 }
10
11 // привязали метод к объекту
12 var userSayHi = bind(user.sayHi, user);
13
14 // понадобилось переопределить метод
15 user.sayHi = function () { alert( 'Новый метод!' ); } ); }
16
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) {
2 return function () {
3 return context[funcName].apply(context, arguments);
4 };
5 }

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) {
02 return function () {
03 return context[funcName].apply(context, arguments);
04 };
05 }
06
07 var user = {
08 sayHi: function () { alert( 'Привет!' ); } ); }
09 }
10
11 var userSayHi = bindLate(user, 'sayHi' );
12
13 user.sayHi = function () { alert( 'Здравствуйте!' ); } ); }
14
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) {
02 return function () {
03 return context[funcName].apply(context, arguments);
04 };
05 }
06
07 // метода нет
08 var user = { };
09
10 // ..а привязка возможна!
11 var userSayHi = bindLate(user, 'sayHi' );
12
13 // по ходу выполнения добавили метод..
14 user.sayHi = function () { alert( 'Привет!' ); } ); }
15
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) {
2 return function () {
3 return context[funcName].apply(context, arguments);
4 };

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Scripting client side JavaScript, jqvery, BackBone

Terms: Scripting client side JavaScript, jqvery, BackBone