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

call vs apply #11

Open
leslie1943 opened this issue Nov 19, 2020 · 0 comments
Open

call vs apply #11

leslie1943 opened this issue Nov 19, 2020 · 0 comments

Comments

@leslie1943
Copy link
Owner

leslie1943 commented Nov 19, 2020

call vs apply

  • callapply 的作用是一样的, 都是为了改变函数在运行时的上下文的, 为了改变函数体内部 this的指向

  • call的参数是按个数传入的, apply的参数是放到一个数组中进行传递

fn.call(this, p1,p2,p3)
fn.apply(this, arguments)
// demo FOR apply 🚀
const dog = {
  name: 'Snoopy',
  say(first, last) {
    console.info(first + ' ' + this.name + ' ' + last)
  },
}

const cat = {
  name: 'Tom',
}

dog.say('Hello', 'World') // Hello Snoopy World
dog.say.apply(cat, ['Hello', 'World']) // Hello Tom World
// demo FOR call 🚀
const dog = {
  name: 'Snoopy',
  say(first, last) {
    console.info(first + ' ' + this.name + ' ' + last)
  },
}

const cat = {
  name: 'Tom',
}

dog.say('Hello', 'World') // Hello Snoopy World
dog.say.call(cat, 'Hello', 'World') // Hello Tom World
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