Or you can just call the sum function when assiging to the res variable like const res = obj.sum(); In this case "this" will refer to the "obj" object not to the global object
Bind is not necessary, a simple call on obj will work
Simplest : console.log(obj.sum())
Soo much valueble. Keep it up brother. Sure these short videos will reach millions of views in some lucky day.
You should also explain what bind does
I don't see any point in asking this question const res = obj.sum() if u directly call like this it ll return the result bcz it has the reference to the obj but res don't have reference to the obj bcz it's a copy of that sum func , to fix it we need not to bind we can simply use obj.sum() will fix it but if they want to access the res() function then we can bind and pass the reference
the most important question is why would you write confusing garbage code like that in the first place? as a senior engineer i would write clear easy to understand functions
they tried to confuse ur friend to make him think like it forms a closure but function inside object doesnt form closure.
It will refer to parent object and with the help of call apply and bind method we can invoke it
Or console.log(res.call(obj))
not sure how all developers are trying to give his out put as correct one because the output is 3 ..... because when called obj.sum -> this will give function defination ....ƒ sum (){ return this.a + this.b } ..... but if doning obj.sum() .... then for the sum() this will points towards the obj object ... then a and b will be 1 and 2 respectively and answer will be 3
Use arrow function
res is not a function then what is the point of invoking it? Only remove the bracket from res and place it as obj.sum() and you will get output as 3
Add paranthesis to sum, when you invoke it, because is a function, not a property.
Line no 9 causes the problem in my opinion because of named function this refers to context of caller please correct if i am wrong
In a normal proper language it should return 3. Proves again how rotten JS is 😂
If let a,b then result is 3??
We can also use arrow function ?
it was good . but not clear visible , in between your talk with sentence will show so code are not clear visible sir , sorry for disturb .
@prajwalchoudhary4824