1

Novice here, trying to grasp how this and arguments differ and in which circumstances one is used instead of the other?

For example, this prototype function will use this to perform some manipulation:

myObj.prototype.myFunc = function(){
  return this.(some manipulation); 
 }   

Similarly, arguments is used to loop through each argument to be manipulated:

function myFunc() {
  for (var i = 0, i < arguments.length; i++){
    return arguments[i].(some manipulation)
  }
} 

Basically in these two examples, the two seem interchangeable, in terms of being accessed as a parameter for further manipulation? I've been reading through various explanations here at Stack Overflow of each alone (like this), but in practice, I'm still quite confused as to which to use when it comes to application/context. When I do searches on the search engines, the "this" is often misunderstood or ignored, likely because of the frequency of its appearance...

Any help much appreciated!

Community
  • 1
  • 1
MTo
  • 11
  • 3
  • 3
    `this` and `arguments` are completely unrelated. Your two functions operate on completely different things. Learn about them independently, don't try to relate them to each other. – Quentin Jul 20 '15 at 21:03
  • arguments will help you get the arguments passed to function and "this" will help you find out the context of method. – ashokd Jul 20 '15 at 21:04

1 Answers1

-1

this is the object on which the function was invoked. For example, if you called a function myObj.foo(), within the scope of foo, this would refer to myObj. The value of this can sometimes be tricky. For example, say you did this:

var fooFn = myObj.foo;
fooFn();

In this case, this wouldn't refer to myObj, but rather to the global window object.

That's this in a nutshell.

As for arguments, it is an array-like object (not a true array) of all the arguments passed to the function. Suppose you have a function function myFn(foo, bar). You can pass more than two arguments to myFn. If you called myFn(1, 2, 3), how would you access the third argument? That's what arguments is for. In this case, arguments would have 3 elements: 1 (corresponding to the foo parameter), 2 (corresponding to the bar parameter), and 3 (the extra parameter).

You can make use of the arguments object to support overloaded versions of functions.

Joe Attardi
  • 3,911
  • 2
  • 32
  • 37
  • 1
    I appreciate the input, regardless! It's a great starting point for a beginner like me. From my perspective, when I see empty a function with empty parentheses for arguments, I'm inclined to think "should I be using `arguments` or `this` here... – MTo Jul 21 '15 at 06:55
  • The `arguments` object has 0-based indices, but your second-to-last paragraph suggests otherwise. – Brian McCutchon Jul 09 '16 at 03:06
  • What? The arguments 1, 2, 3 are values, not indices. I assumed I didn't need to specify that it was zero-based indexing, since I did say it was an array-like object. – Joe Attardi Jul 09 '16 at 17:08