Home > Article > Web Front-end > Detailed explanation of functions and object instances with duplicate names in JavaScript
This article mainly introduces to you the relevant content about functions and objects with duplicate names in JavaScript, and shares it for your reference and study. I hope it can help everyone.
JavaScript allows repeated declaration of variables, and the later declaration overwrites the previous one.
var a = 1; var a = 'x'; console.log(a); //输出'x'
JavaScript allows repeated definition of functions.
JavaScript does not have the concept of overloading. It only distinguishes functions based on their names.
The function with the same name defined later overwrites the previous one, regardless of the parameters.
function test() { console.log("test"); } test(); //输出 "test arg0 + undefined" function test(arg1) { console.log("test arg" + arguments.length + " + " + arg1); } test(1,2); //输出 "test arg2 + 1"
If the number of actual parameters is less than the formal parameters, then the remaining default assignments are undefined; if the number of actual parameters passed is more than the number of formal parameters, then all will be passed in, only However, there is no corresponding formal parameter that can be referenced (but you can use arguments to get the remaining parameters)
function test(arg1) { for(var i=0; i<arguments.length; i++) { console.log(arguments[i]); } } test(1,2); //输出 1 2
When a variable has the same name as a function, the variable takes effect
This involves variables Pre-parsing of functions and functions:
- Variable declarations will be placed on top, and function declarations will also be placed on top and declared before variables.
- When the declaration of a variable and the assignment statement are written together, the JS engine will split it into two parts: the declaration and the assignment. The declaration will be placed at the top and the assignment will remain at the original position.
- Declared variables will not be declared again.
var a = 100; function a() { return "function"; } console.log(a); //输出 100 console.log(a()); /* 报错 Uncaught TypeError: a is not a function (anonymous function) @test.html:9 */
There are two kinds of functions in JS, one is an ordinary function and the other is a function object. The following is a "function object", which actually declares an anonymous function and then assigns the function's init method to the variable.
var a = 100; var a = function() { return "function"; } console.log(a); /* 输出 function() { return "function"; } */ console.log(a()); //输出 "function"
The function has the same name as the internal variable
Define an ordinary function, that is, define a key under the window variable, its name is the function name, and its value is the address of the function. This inside the function points to the window object.
function a() { console.log(this); //输出 window{...} this.a = 1; //即 window.a = 1,此时window下的function a已经被该变量覆盖了。 var a = 5; //下面的这几个变量都是局部变量,仅在花括号范围内有效。 a = 10; var v = "value" return "function"; } console.log(a); //输出 function a {...} console.log(a()); //输出 "function" console.log(a); //输出 1 console.log(v); /* 输出 Uncaught ReferenceError: v is not defined (anonymous function) @ mycolor.html:15 */
Related recommendations:
jQuery and Ajax to implement duplicate user names
About how to solve js duplicate names The problem of sequential calling of methods
Look at the initialization method from the duplicate names of JavaScript functions_javascript skills
The above is the detailed content of Detailed explanation of functions and object instances with duplicate names in JavaScript. For more information, please follow other related articles on the PHP Chinese website!