


js multiple variable definitions (object literals, array literals and function literals)_javascript skills
The code is the same as below.
var obj=new Object();
obj.x=new Array(1,2);
obj.y=23;
Test:
for(var i in obj) alert(obj[i]);
Function literal: It is an expression rather than a statement.
The following example:
(function(){
document.write("some script code");
})()
var a=(function(s){return s})("abc");
alert( a);
var b=function(s){return s};
alert(b("abc"));
How to explain this
Everyone should remember This way of writing
var a=function (){}
So how to run a, then it is a()
In the same way, we do not save it through the variable a, so how to write it, it is
function(){}()
But you will find that this is wrong
Because when the parsing engine parses, it finds that it has judged that the function has ended
It does not regard that function as a block To run
, then adding () forces the function block as a block
1. What is an anonymous function
There are generally three ways to define a function in Javascript: :
Function keyword (function) statement:
function fnMethodName(x){alert(x);}Function Literals:
var fnMethodName = function(x){alert (x);}Function() constructor:
var fnMethodName = new Function('x','alert(x);') The above three methods define the same method function fnMethodName, the first one is the most commonly used The latter two methods copy a function to the variable fnMethodName, and this function has no name, that is, an anonymous function. In fact, quite a few languages have anonymous functions.
2. The difference between function literal and Function() constructor
Although function literal is an anonymous function, the syntax allows you to specify any function name for it , you can call itself when writing a recursive function, but not using the Function() constructor.
var f = function fact(x) {
if (x < = 1) return 1;
else return x*fact(x-1);
};
Function( ) constructor allows dynamic creation and compilation of Javascript code at runtime. In this way it is similar to the global function eval().
The Function() constructor parses the function body and creates a new function object each time it is executed. Therefore, the efficiency of calling the Function() constructor in a loop or frequently executed function is very low. In contrast, function literals are not recompiled every time they are encountered.
When you create a function using the Function() constructor, it does not follow the typical scope. It always executes it as a top-level function.
var y = "global";
function constructFunction() {
var y = "local";
return new Function("return y"); // Unable to obtain local variable}
alert(constructFunction()()); // Output "global" function literal:
As long as it is an expression syntax, the script host will think that function is a literal function. If nothing is added and it starts with function, it will be considered a function. Declaration, write function into an expression, such as four arithmetic operations, the host will also treat it as a direct quantity, as follows:
var a = 10 function(){
return 5;
}();
A bit exaggerated, As follows:
(function(){
alert(1);
} ) ( );
( function(){
alert(2);
} ( ) ) ;
void function(){
alert(3);
}()
0, function(){
alert(4);
}();
-function(){
alert(5);
}();
function(){
alert(6);
}();
!function(){
alert(7);
}();
~function(){
alert(8);
}();
typeof function(){
alert (9);
}();
There are many ways to define functions in js, and function literals are one of them. For example, var fun = function(){}, if function is not assigned to fun, then it is an anonymous function.
Okay, let’s see how the anonymous function is called.
1. The function call that returns the value after execution
//Method 1, call the function and get the return value. The coercion operator causes the function call to execute
(function(x,y){
alert(x y);
return x y;
}(3,4));
/ /Method 2: Call the function and get the return value. Force the function to be executed directly and then return a reference. The reference is called and executed
(function(x,y){
alert(x y);
return x y;
})(3,4) ;
2. Ignore the return value after execution
//Method 3, call the function and ignore the return value
void function(x) {
x = x-1;
alert(x);
}(9);
Well, finally look at the wrong calling method
//Wrong calling method
function(x,y){
alert(x y);
return x y;
}(3,4);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Python, variables can be understood as containers for storing data. When we need to use or manipulate data, we can define variables to store the data so that we can easily call and process the data. The following will introduce how to define variables in Python. 1. Naming rules In Python, the naming rules for variables are very flexible and usually need to follow the following rules: variable names consist of letters, underscores and numbers, and the first part cannot be a number. Variable names can use uppercase and lowercase letters, but Python is case-sensitive. variable name

Golang is a fast, efficient, and modern programming language that automatically checks types at compile time and has features such as concurrency and memory safety, so it is favored by more and more developers. In Golang, we often need to use functions to encapsulate business logic, and the assignment method when defining variables in functions is a common problem. This article will explain this problem in detail and analyze the differences. Variable definition In Golang, variables can be defined in two ways: var and :=. Among them, var square

PHP is a widely used programming language with excellent scalability and practicality. In PHP, variables and constants are two very important concepts. They can be used to store and represent values and store important information. In this article, we will introduce in detail how to define variables and constants in PHP to help beginners get started quickly. 1. Define variables A variable is a name or identifier used to store a value. In PHP, the definition of variables can be divided into three steps: variable declaration, variable assignment and variable use. Below we detail

Duplicate definition errors of function variables in Python are a common problem. When a variable with the same name is repeatedly defined in a function, Python will throw a "localvariable'xxxx'redefined" error. This error is usually caused by duplication of variable names inside and outside the function. In Python, variable scope is divided into local scope and global scope. When a variable is defined in a function, the variable defaults to a local variable and can only be used in that function.

Overview of specifications and techniques for variable definition in Golang: In Golang, variables are the most basic data storage unit in the program. Proper use of variable definition conventions and techniques can improve code readability, maintainability, and performance. This article will introduce some specifications and techniques for variable definition in Golang, and provide specific code examples. Naming conventions for variables: In Golang, there are certain conventions for naming variables. Variable names should use camelCase, with the first letter lowercase. If it is a private variable, it should be named in camel case.

In C++ programming, you sometimes encounter a common error, that is, the "a defined variable must be at the top" error. This is usually caused by a variable being defined in an incorrect location. In this article, we will discuss how to fix this error. In C++, variables usually need to be defined at the beginning of the function body or scope. If you define a variable at the bottom before calling it, a compilation error "A defined variable must be at the top" will appear. The solution to this error is to move the variable definition to a function or action

Common problems and solutions for variable definition in Golang language When programming in Golang language, variable definition is a basic and common operation. However, since Golang has some special rules and regulations, we may encounter some problems during variable definition. This article will introduce common problems and give corresponding solutions and code examples. Problem 1: Variables are declared but not used. In Golang, if we declare a variable but do not use it in subsequent programs, the compiler

How to solve C++ compilation error: 'operatingon'variable'thatisbeingdefined'? In C++ programming, sometimes we encounter an error message: 'operatingon'variable'thatisbeingdefined'. This error message indicates that we are operating on a variable while defining it, which is not allowed. In this article we will discuss this
