简体   繁体   中英

What's the purpose of javascript parameters that are overwritten in the function?

I have a code snippet to include in a project, and I'd like to know if there's a purpose to why it was written like this or if I should clean it up. Is there an optimization trick I don't know about?

Here's the (sanitized) code snippet. Notice a,b,c,d passed in and then assigned in the function.

(function (a, b, c, d) {
    a = '//url/to/js/file.js';
    b = document; c = 'script'; d = b.createElement(c); d.src = a; d.type = 'text/java' + c; d.async = true;
    a = b.getElementsByTagName(c)[0]; a.parentNode.insertBefore(d, a);
})();

I found this answer after writing this question up. Is that the only reason?

That way you can make a local scoped variable without explicitly declaring it using var statement.

IIRC it's commonly used by code minifiers.

Just adding a little detail to the answer in the linked question. Sometimes when working with some API's, its functions will validate functions you pass to them. Sometimes, due to this, they'll only call your own functions if they have the right amount of parameters declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM