basically the difference is the meaning of the arguments.
assume f = function(x,y){};
f.apply(obj, [xValue, yValue]);
So in this case the array will become the arguments on function f.
f.call(obj, xValue, yValue);
So in this case the "rest" arguments will become the arguments on function f.
What benefis you get from this?
well basically use apply when you the arguments as an array as in a delegation.
f -> f1
because you don't need to extract the arguments from the array and the build
the function call and refactor if the f1 changes the number of arguments.
when you have to build the call manually use call, in most cases its the best
way because the call feels more natural.
besides the arguments management this two methods are the same.
On Jul 16, 2011, at 6:14 PM, Matthew Bramer wrote:
> I've been watching some appendTo videos and I came across a video with this
> code sample in it:
>
> var obj1 = {
> ping: "pong"
> },
> myfunc = function(append) {
> console.log([this.ping, append].join(" "));
>
> }
>
> myfunc.apply(obj1, ["something", "else"]);
> pong something
>
>
>
>
> var obj1 = {
> ping: "pong"
> },
> myfunc = function(append) {
> console.log([this.ping, append].join(" "));
>
> }
>
> myfunc.call(obj1, ["something", "else"]);
> pong something,else
>
> I played around with these two methods and realized .apply will only accept
> an array. However, .call will accept an array as well. What is the
> difference between these two methods and why would I use .apply when .call
> allows me to pass strings as well as arrays?
>
> Cheers,
> Matt
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]