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, 2.call/apply/bind 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 !!

 

obj1.obj2.foo means foo is called in context of obj2

if you do bar = o1.o2.foo 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() { foo.call(obj); }

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.

obj1.foo.call( obj2 ); // 3
obj2.foo.call( 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.

 

 

Advertisements
No comments yet

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: