Skip to content

Kyle’s JS – Points

March 3, 2016

If strict mode is in effect, the global object is not eligible for the default binding, so the this is instead set to undefined:

this Binding Rules 4: default (global) , implicit (fn call) , explicit (bind, call apply) , new binding.

Sequence is: 1. new fn call, 3. obj.fn call 4. default

safe object:

// our   empty object
var ø = Object.create( null );

Concept of soft binding so that global object does not get used ever !! means foo is called in context of obj2

if you do bar = and then you call bar() – bar is called without context – global or undefined in case of strict

Explicit binding is when you use call or apply and pass the first parameter – as the object context.wrap a function with a hard binding

var bb = function() {; }

bb(); // in case of global or set timeout, will always use the right context for calling object.

By calling foo(..) with new in front of it, we’ve constructed a new object and set that new object as the this for the call of foo(..). So new is the final way that a function call’s this can be bound. We’ll call this new binding.

bind(..) returns a new function that is hardcoded to call the original function with the this context set as you specified. obj2 ); // 3 obj1 ); // 2

So, explicit binding takes precedence over implicit binding, which means you should ask first if explicit binding applies before checking for implicit binding.



No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: