What is the normative (best) use of Javascript arguments object [closed]

JS offers the ‘arguments’ object inside of functions to represent all passed arguments. Is there any broadly accepted concept for when/where this should be used?

I’ve seen items like this in the ‘wild’ that seem like code smell to me:

function myFxn () {
  var myVar = arguments[0];
  // do something with myVar
}

this is a very simplified example, but it illustrates what seems to me to be a poor coding practice. I would prefer

function myFxn (myVar) {
  // do something with myVar
}

A colleague used it in a number of places where they only wanted to name the ‘required’ args (‘required’ by the implemenation ) but omit the ‘optional’ ones:

function myFxn (myReqVar) {
  var myOptVar = (arguments[1] !== undefined) ? arguments[1] : false;
  // do something with vars
}

This didn’t seem wise on grounds of reduced readability and maintainability. It seems to me to create functions with confusing interfaces that must rely on the developer’s knowledge of its intent or external documentation to understand and use.

Is this just a matter of opinion, or is there a preponderance of opinion on this?

2

I think you are right, the best practice would be to use parameters over var paramName = arguments[0] . It is cleaner, easier to read and it shows the intent better. As a general rule of thumb the arguments object should be used over params under a few scenarios:

There can be N number of params:

    function sum(){
    var total = 0;
    for (var i = 0; i < arguments.length; i++) {
        total += arguments[i];
    }
    return total;
}

If you want to generically call other functions with the passed in arguments:

function other() {
    otherFunction.apply(this, arguments);
}

vs

function other(arg1, arg2, arg3) {
    otherFunction.call(this, arg1, arg2, arg3);
}

Using reusable functions that use arguments:

Here is an example of a generic argument validating :
https://codereview.stackexchange.com/questions/31593/throwing-error-if-settings-arguments-are-undefined-is-this-a-good-breakout-fun/31602#31602

1

The arguments object is useful for mimicking the behavior or overloaded methods, or creating functions that accept arbitrary numbers of arguments. A good native example is the Array#push method:

var x = [];
x.push(1, 2);

Is the equivalent of:

var x = [];
x.push(1);
x.push(2);

A good example of mimicking method overloading is the jQuery on method:

$("#foo").on("click", function(event) {
    // process the click event when "#foo" gets clicked
});

$("#foo").on("click", ".bar", function(event) {
    // process click event when any element with "bar" as one of its
    // classes gets clicked inside "#foo"
});

It’s the same method name (on) but the different numbers and types of arguments change the behavior of the method, giving you the impression that the on method has two overloads.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật