javascript - Why use this obj.method.call(obj, arg) and not obj.method(arg)? -


i see pattern sometimes...

obj.method.call(obj, arg)   

and don't understand why different from...

obj.method(arg)   

why use first pattern?

my goodness, seems have generated lot of discussion :)

just clarify, asking case when object method on same first argument of call() (or maybe @felixkling put better: when owner same receiver). i'm not asking obj.method.call(obj2, arg).


as mentioned below, case when function bound fails differentiate 2 methods.

o2={p2:3}; o={p1:(function(){return this.p2}).bind(o2), p2:2}; o.p1() === o.p1.call(o) // true   

edit

i guess source of idiom pattern used rebind object's methods...

obj.method.apply(obj, arguments) 

why use obj.method.call(obj, arg) , not obj.method(arg)?

there no practical reason this.

in both cases this refer obj, assuming function not bound different value (in case this bound value anyway).


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -