Skip to content

Instantly share code, notes, and snippets.

@liyaodong
Created April 7, 2016 07:27
Show Gist options
  • Save liyaodong/857bd98558aed3c31a5441a564723866 to your computer and use it in GitHub Desktop.
Save liyaodong/857bd98558aed3c31a5441a564723866 to your computer and use it in GitHub Desktop.

###1 The Method Invocation Pattern obj.action()

this -> obj

###2 The Function Invocation Pattern fun()

this -> 向上作用域查找 this, 直到最后的 global object (window object in browser env, module.exports in node env)

###3 The Constructor Invocation Pattern new Foo()

this -> the new instance object

###4 The Apply Invocation Pattern // invoke function

fun.apply(this, params array)

fun.call(this, arg1, arg2)

// return a new function

fun.bind(this, arg1, arg2)

this -> dynamic by apply() and call(), bind() first param.

new in es6

const fun = () => console.log(this);

this -> auto bind this to function write scope, not invoke scope.

See more

http://bdcampbell.net/javascript/book/javascript_the_good_parts.pdf

Chapter 4: Functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment