01 November 2014

Origin https://www.youtube.com/watch?v=fjJoX9F_F5g

This in window context

WindowThis


This In Global Function

This In Global Function


What is this

What is this


This in Object

This in Object


Three methods to change context

Three methods to change context

Method 1

  var obj = {
    foo : function(){
      console.log(this === window);
    }
  };
  //call
  obj.foo.call(window);

Method 2

  var obj = {
    foo : function(){
      console.log(this === window);
    }
  };
  //call
  obj.foo.apply(window);

Method 3

  var obj = {
    foo : function(){
      console.log(this === window);
    }
  };
  //call
  var windowFoo = obj.foo.bind(window);
  windowFoo();

Bind to change Context

Bind to change Context


Element context in event

Element context in event


Get parent context by reference variable**

Get parent context by reference variable


Get parent context by methd bind

Get parent context by methd bind



blog comments powered by Disqus