HomeBlog
JavaScript Function Arguments Assignment - 羡辙杂俎
2015.07.29 09:30:22
JavaScript Function Arguments Assignment

arguments is a special variable passed to all functions so that actual parameters are accessible even when they are not declared as formal parameters.

function a() {
    console.log(arguments[0], arguments[1]); // a b

}
a('a', 'b');

In most cases, arguments is an alias name for formal parameters.

function a(x, y) {
    x = 1;
    arguments[1] = 2;
    console.log(arguments[0], arguments[1], x, y); // 1 2 1 2

}
a('a', 'b');

Here’s how it goes interesting.

a('a', undefined); // 1 2 1 2

a('a');            // 1 2 1 undefined

When an actual parameter is not passed to the function, its behavior is not exactly the same as passing an undefined. A more profound explanation under the hood should be given once I knew more about JavaScript.

博文对你有帮助吗?如果有的话,要不要送我一本书呢?:heart_eyes:

微信公众号“羡辙部落格”(xianzheblog)提供本站优质非技术类博文推送。

Creative Commons License
Every blog post in this site by Wenli Zhang is licensed under a CC 3.0 License, based on a work at Github. You can see the source code of this blog site at github.com/Ovilia/blog.

Comments

comments powered by Disqus