Prototype chain inheritance and apply() call() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Prototype chain inheritance and apply() call()

Difference and method of prototype inheritance and apply() inheritance

17th Sep 2016, 3:33 PM
Chong
1 Answer
+ 1
apply() lets you set any value to implicit function parameter named `this`, which is supposed to point to method owner. When you execute `myObject.myMethod(arg1, arg2)`, and this method actually is not defined, the interpreter will look for method with name `myMethod` in object's prototype, than in prototype's prototype and so on. If it is found in a prototype, something like this (very roughly) will be executed: `myObject.__proto__.myMethod.apply(myObject. [arg1, arg2])` apply() must be used because otherwise `this` inside called method will point to prototype, not our object. You can call any function from object's method and give it the object in `this` variable using apply(). Such calls look like customized inheritance.
17th Sep 2016, 5:14 PM
Andriy Maletsky
Andriy Maletsky - avatar