最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

naming - Is it ok to use an argument named "arguments" in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I'm writing a library that exposes the following function

function call(instance, func, arguments) {
  return {
    call: {
      instance: instance,
      func: func,
      arguments: arguments
    }
  }
}

Is the name arguments ok to use? It's really the best name for it, but I don't want to clash with the builtin variable. It works in Node. Will this work in all browsers and JavaScript environments?

Edit:

Note that the above function does work as expected in Node. What I'm wondering is whether this will work everywhere.

I'd rather not rename it unless there's a technical reason to. I'd really like this external-facing function and the docs to reflect this parameter name, because the returned object is going to be serialized as JSON and used across languages.

I'm writing a library that exposes the following function

function call(instance, func, arguments) {
  return {
    call: {
      instance: instance,
      func: func,
      arguments: arguments
    }
  }
}

Is the name arguments ok to use? It's really the best name for it, but I don't want to clash with the builtin variable. It works in Node. Will this work in all browsers and JavaScript environments?

Edit:

Note that the above function does work as expected in Node. What I'm wondering is whether this will work everywhere.

I'd rather not rename it unless there's a technical reason to. I'd really like this external-facing function and the docs to reflect this parameter name, because the returned object is going to be serialized as JSON and used across languages.

Share Improve this question edited Nov 29, 2014 at 21:06 Joe asked Nov 29, 2014 at 20:50 JoeJoe 16.8k12 gold badges65 silver badges76 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It is okay to use the name arguments in "sloppy mode", but not remended.

It is forbidden in strict mode, which all new code should use if the author cares about code quality.

function a(arguments) {
    console.log(arguments);
}
a(1);
// Prints "1"

(function () {
    'use strict';
    function a(arguments) {
        console.log(arguments);
    }
    a(1);
}());
// Uncaught SyntaxError: Unexpected eval or arguments in strict mode

This is akin to naming a variable "Object". Maybe in some obscure context it makes sense to use that name, but by doing so you lose access to the global Object object and useful methods on it like Object.keys. Similarly, by making this poor naming decision, you can no longer manipulate the arguments object, which is varargs in JS.

In the interest of improving the maintainability of your code, it is best to avoid creating ticking time bombs like this one. There is a good reason why it is not allowed in strict mode: It is likely to cause misery for anyone who wants to use arguments as it is typically used.

It looks like that is taken. arguments is a quick, array-like way to get all the arguments of a function. See this explaining it.

Maybe you could use parameters instead?

EDIT: To see how this variable works, I wrote this:

function sayHelloTo(objectToSayHiTo){
  alert("Hello "+arguments[0]+"!");
}

sayHelloTo("world");

EDIT #2:

Apparently, it will not be affected:

function sendGreetings(name, arguments){
    alert("I'm "+name+"!");
    for(var count=0;count<arguments.length;count++){
      alert("Hello "+arguments[count]+"!");
    }
}

sendGreetings("kittycat3141", ["world", "StackExchange", "Joe"]);

However, I haven't tried all browsers yet.

EDIT #3 I have tried 5 major browsers (IE, Chrome, Safari, Firefox, and Opera) and the code above works properly. However, as iCobot said in the ments, it is best not to use builtin names because it can save you confusion and frustration later, for you and anyone reading the code.

发布评论

评论列表(0)

  1. 暂无评论