InterviewSolution
| 1. |
What is prototypal inheritance ? |
|
Answer» EVERY function in JavaScript have call, APPLY and bind methods. These methods can be used to set the CUSTOM value of “this” to the execution context of the function. call Now, in addNumbers we have this.num. But how do we pass the value obj.num to it. We need to pass it a context, which means the value of “this”. We will do this my call method by passing a first argument as obj, so the “this” is the obj now. let obj = {num: 3}; let addNumbers = function(a, b, c){ return this.num + a + b + c; }; console.log(addNumbers.call(obj, 1, 4, 6)); //14apply bind |
|