Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this的指向 #3

Open
sihai00 opened this issue Jul 19, 2019 · 0 comments
Open

this的指向 #3

sihai00 opened this issue Jul 19, 2019 · 0 comments
Assignees

Comments

@sihai00
Copy link
Owner

sihai00 commented Jul 19, 2019

  1. 默认绑定:this绑定windows
  2. 隐式绑定:对象的方法
  3. 显式绑定:apply、call、bind
  4. new绑定
// 1. 默认绑定
function foo(){
  console.info(this.a)
}
var a = 'hello'
foo()

// 2.隐式绑定
var obj = {
  a: 1,
  foo:  function() { console.log(this.a) }
}
obj.foo()

// 3. 显式绑定
var obj = {
  a: 1,
  foo:  function() { console.log(this.a) }
}
obj.foo.call({a: 2})
obj.foo.apply({a: 2})
var b = obj.foo.bind({a: 2})
b()

// 4. new绑定
function a(name){
  this.name = name
}
new a('a')
@sihai00 sihai00 self-assigned this Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant