Home  >  Article  >  Web Front-end  >  Tutorial on defining formal parameters in a function but not passing actual parameters

Tutorial on defining formal parameters in a function but not passing actual parameters

零下一度
零下一度Original
2017-06-25 09:19:282737browse

1. Defining formal parameters in a function but not passing actual parameters is equivalent to defining a variable but not assigning a value, so a in the following is undefined

1 function test(a){2     console.log(a)//undefined3 };4 test();

2. In IIFE, window is passed as an actual parameter, which avoids searching for the window globally every time when executing the code, thereby improving efficiency. But why do we need to specify an undefined in the formal parameter? As mentioned above, Defining a formal parameter in a function is equivalent to defining it but not assigning a value, so undefined or undefined in this immediately executed function. Therefore, even if the value of undefined is modified globally, it will not affect the undefined in this self-executing function. However, in Chrome 59 and above, and Firefox 53 and above, undefined is not allowed to be modified, and all output is undefined. Under IE8, undefined values ​​can be modified, and the output is 8 and undefined, so passing formal parameters can ensure to the greatest extent the impact of externally modified variables on the internal function in various browsers. There must be parentheses before IIFE otherwise an error will be reported.

1 //在ie8中2 var undefined = 8;3 (function( window , undefined ){4     console.log(window.undefined) //85     console.log(undefined); //undefined6 })(window);
//在chrome和Firefox中var undefined = 8;
(function( window , undefined ){
    console.log(window.undefined) //undefinedconsole.log(undefined); //undefined})(window)

The above is the detailed content of Tutorial on defining formal parameters in a function but not passing actual parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn