


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 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);

在Python中,变量可以理解为存储数据的容器。当我们需要使用或操作数据时,可以通过定义变量来存储数据,从而方便地调用和处理这些数据。下面将介绍Python中定义变量的方法。一、命名规则在Python中,变量的命名规则非常灵活,通常需要遵循以下规则:变量名由字母、下划线和数字组成,首位不能为数字。变量名可以使用大小写字母,但Python是区分大小写的。变量名

Golang是一种快速、高效、现代化的编程语言,它在编译时会自动检查类型,并且具有并发性和内存安全性等特点,因此被越来越多的开发者所青睐。在Golang中,我们经常需要使用函数来封装业务逻辑,而函数中的变量定义时的赋值方法是一个常见的问题,本文将详细讲解这个问题并分析其中的区别。变量定义在Golang中,可以使用var和:=两种方式来定义变量。其中,var方

PHP是一种被广泛使用的编程语言,具有卓越的可扩展性和实用性。在PHP中,变量和常量是两种十分重要的概念,它们可以用来存储和表示值以及存储重要的信息。在这篇文章中,我们将会详细介绍如何在PHP中定义变量和常量,以帮助初学者快速上手。一、定义变量变量是用于存储值的名字或标识符。在PHP中,变量的定义可以分为三个步骤:变量的声明、变量的赋值和使用变量。下面我们详

Python的函数变量重复定义错误是一个常见问题,当一个函数中重复定义了相同名称的变量时,Python会抛出“localvariable'xxxx'redefined”错误。这个错误通常是由于函数内部的变量名和外部的变量名重复导致的。在Python中,变量作用域分为局部作用域和全局作用域,当在一个函数中定义变量时,该变量默认为局部变量,并且只能在该函数

在C++编程中,有时会遇到一个常见的错误,即“一个定义的变量必须在最上面”的错误。这通常是由于变量定义的位置不正确导致的。在本文中,我们将讨论如何修复这个错误。在C++中,变量的定义通常需要在函数体或作用域的开始处进行。如果你定义的变量放在下面,而在调用之前,则会出现“一个定义的变量必须在最上面”的编译错误。出现这个错误的解决方案就是将变量定义移到函数或作用

Golang中变量定义的规范与技巧概述:在Golang中,变量是程序中最基本的数据存储单元。正确使用变量定义的规范和技巧可以提高代码的可读性、可维护性和性能。本文将介绍一些Golang中变量定义的规范和技巧,并提供具体的代码示例。变量的命名规范:在Golang中,变量的命名是有一定规范的。变量名应该使用驼峰命名法,首字母小写。如果是私有变量,应该使用驼峰命名

解决C++编译错误:'operatingon'variable'thatisbeingdefined',如何解决?在C++编程中,有时候我们会遭遇到一个错误信息:'operatingon'variable'thatisbeingdefined'。这个错误信息指明我们在定义变量的同时对其进行了操作,这是不被允许的。在本文中,我们将讨论这

Golang语言中变量定义的常见问题及解决方法在使用Golang语言进行编程时,变量的定义是一个基础且常见的操作。然而,由于Golang有一些特殊的规则和规定,我们可能在变量定义过程中遇到一些问题。本文将针对常见问题进行介绍,并给出相应的解决方法和代码示例。问题一:变量声明但未使用在Golang中,如果我们声明了一个变量,但在后续的程序中并未使用该变量,编译


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
