0

I have tried so much to understand call/bind/apply but still am so much confused to find why we use call/bind/apply in javascript and is it the same as object prototyping?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Nothing in common with object prototyping. `call`/`bind`/`apply` are only different ways to execute a function and supply it with context and parameters. – VLAZ Mar 23 '20 at 14:19
  • I read an article where the words are: "it allows us to call a function with a given this context and arguments. They let us call a function and have access to the properties of another function or object. We can borrow methods of one object’s prototype and use it to another" Now I am confused that we do the same in object prototyping that we create a Constructor function with "this" and we create different objects with that , with different "this" references. – Shahroz Ahmed Mar 23 '20 at 14:22
  • The prototype is not directly related. A prototype just defines what methods a type of object would have. And "methods" in JS are functions which execute with the context set to the instance you're calling them from: `foo.someMethod()` and `bar.someMethod()` can share the code for `someMethod()` but the context (the `this` keyword) would be different. But you can also have a proper function (not a method) that uses `this` within its body and execute it against different instances like `someFunction.call(foo)` and `someFunction.call(bar)`. – VLAZ Mar 23 '20 at 14:26
  • So, simply put `call`/`apply`/`bind` are not directly related to prototype inheritance, but prototype inheritance relies on the [`this` keyword](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work), thus you can "borrow" a method from one instance to execute against another `cat.meow.call(dog)` is valid way to transiently call `dog.meow()` without `dog` actually having this method. – VLAZ Mar 23 '20 at 14:29
  • ok, I got the point. Thank you – Shahroz Ahmed Mar 23 '20 at 15:22

0 Answers0