简体   繁体   中英

Chaining functions directly to function(){}

So ever once in a while, just for fun, I check out the source of Facebook to find all kinds of crazy javascript.

So in the keyup of the textarea on the facebook chat, i found:

Bootloader.loadComponents(["control-textarea"], function () {
    TextAreaControl.getInstance(this)
}.bind(this));

This didn't seem to interest me until I relized that .bind() is added directly to that function.

I didn't know you can do that.

Can any function be after to another function(){} just like that? Or is there a special way you need to do this.

I know you can chain stuff like "How are you doing!".replace(/\\w{3}/g,'').toUpperCase() , but didn't think you chain directly to the function(){} itself.

.bind is a property available on Function objects in ES5 environments, or where .bind has been added to Function.prototype . It's just like any other property available on an object or via the prototype chain.

The .bind method returns a new function with its calling context (and arguments if provided) permanently bound to that new function. The new function invokes the code body of the original function, so aside from the bound context and args, the behavior is otherwise the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM