


The difference between common functions defined in javascript_javascript skills
The differences between the more common functions defined in JavaScript are mainly explained through the following three aspects. Friends in need can refer to it
1: Call keyword function to construct
For example:
function distance(x1,x2,y1,y2) { var dx=x2-x1; var dy=y2-y1; return Math.sqrt(dx*dx+dy*dy); }
2: Use Function() constructor
For example:
var f=new Function*"x","y","return x*y");
This line of code creates a new function that is essentially equivalent to a function defined using the syntax you are familiar with:
function f(x,y) { return x*y; }
The Functino() constructor can accept any number of string parameters. Its last parameter is the body of the function, which can contain any JavaScript statements separated by semicolons. The other parameters are strings used to describe the names of the formal parameters to be defined by the function. If the function you define has no parameters, you can just pass a string (the body of the function) to the constructor.
Note that none of the parameters passed to the constructor Function() specify the name of the function it is creating. Unnamed functions created with the Function() constructor are sometimes called "anonymous functions".
You may be very curious to know what the Function() constructor is used for. Why can't we just use function statements to define all functions? The reason is that the Function() constructor allows us to dynamically build and compile a function without limiting us to the precompiled function body of the function statement. The side effect of this is that every time a function is called, the Function() constructor must compile it. Therefore, we should not call this constructor frequently in loop bodies or in frequently used functions.
Another reason to use the Function() constructor is that it can define the function as part of a JavaScript expression instead of defining it as a statement. In this case, using it is more convenient and even elegant. .
3: Function literal
A function literal is an expression that can define an anonymous function. The syntax of a function literal is very similar to that of a function statement, except that it is used as an expression rather than a statement, and there is no need to specify a function name. The following three lines of code define three basically the same functions using the function() statement, the Function() constructor, and the function literal:
function f(x){return x*x}; var f=new Function("x","return x*x;"); var f=function(x){reurn x*x};
Although the function literal creates an unnamed function, its syntax also specifies that it can specify a function name, which is very useful when writing a recursive function that calls itself.
For example:
var f=function fact(x){if(x<=1)return 1;else return x*fact(x-1);};
The above code defines an unnamed function and stores a reference to it in the variable f. It does not actually create a function named fact(), it just allows the function body to refer to itself by this name. Note, however, that this named function literal was not implemented correctly in versions prior to JavaScript 1.5.
The usage of function literals is very similar to the method of creating functions using the Function() constructor. Since they are created by JavaScript expressions rather than statements, the way they are used is more flexible, especially for functions that are only used once and do not need to be named. For example, a function specified using a function literal expression can be stored in a variable, passed to other functions, or even called directly:
a[0]=function(x){return x*x;};//定义一个函数并保存它 a.sort(function(a,b){return a-b;});//定义一个函数;把它传递给另一个函数 var tensquared=(function(x){return x*x;})(10);
和Function()构造函数一样,函数直接量创建的是未命名函数,而且不会自动地将这个函数存储在属性中。但是,比起Function()构造函数来说,函数直接量有一个重要的优点。由Function()构造函数创建的函数的主体必须用一个字符串说明,用这种方式来表达一个长而复杂的函数是狠笨拙的。但是函数直接量的主体使用的却是标准的JavaScript语法。而且函数直接量只被解析一次,而作为字符串传递给Function()构造函数的JavaScript代码则在每次调用构造函数时只需被解析一次和编译一次。
在JavaScript1.1中,可以使用构造函数Function()来定义函数,在JavaScript1.2和其后的版本中,还可以使用函数直接量来构造函数。你应该注意这两种方法之间的重要差别。
首先,构造函数Function()允许在运行时动态地创建和编译JavaScript代码。但是函数直接量却是函数结构的一个静态部分,就像function语句一样。
其次,作为第一个差别的必然结果,每次调用构造函数Function()时都会解析函数体并且创建一个新东汉数对象。如果对构造函数的调用出现在一个循环中,或者出现在一个经常被调用的函数中,这种方法的效率非常低。另一个方面,函数直接量或出现在循环和函数中的嵌套函数不是在每次调用时都被重新编译,而且每当遇到一个函数直接量时也不创建一个新的函数对象。
Function()构造函数和函数之间量之间的第三点差别是,使用构造函数Function()创建的函数不使用词法作用域,相反的,它们总是被当作顶级函数来编译,就像下面代码所说明的那样:
var y="global"; function constructFunction() { var y="local"; return new Function("return y");//不捕捉局部作用域。 } //这行代码将显示"global",因为Function()构造函数返回的函数并不使用局部作用域。 //假如使用一个函数直接量,这行代码则可能显示"local"。 alert(constructFunction());

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

JavaScript Function Asynchronous Programming: Essential Skills for Handling Complex Tasks Introduction: In modern front-end development, handling complex tasks has become an indispensable part. JavaScript function asynchronous programming skills are the key to solving these complex tasks. This article will introduce the basic concepts and common practical methods of JavaScript function asynchronous programming, and provide specific code examples to help readers better understand and use these techniques. 1. Basic concepts of asynchronous programming In traditional synchronous programming, the code is

In modern web applications, implementing web page navigation and routing is a very important part. Using JavaScript functions to implement this function can make our web applications more flexible, scalable and user-friendly. This article will introduce how to use JavaScript functions to implement web page navigation and routing, and provide specific code examples. Implementing web page navigation For a web application, web page navigation is the most frequently operated part by users. When a user clicks on the page

Real-time updates of data visualization using JavaScript functions With the development of data science and artificial intelligence, data visualization has become an important data analysis and display tool. By visualizing data, we can understand the relationships and trends between data more intuitively. In web development, JavaScript is a commonly used scripting language with powerful data processing and dynamic interaction functions. This article will introduce how to use JavaScript functions to achieve real-time updates of data visualization, and show the specific

JavaScript is a scripting language that can be used to add interactive effects to web pages. Among them, image carousel and slideshow effects are common web page animation effects. This article will introduce how to use JavaScript functions to achieve these two effects and provide specific code examples. Picture carousel Picture carousel is an effect that plays multiple pictures in turn in a certain way. When implementing image carousels, JavaScript timers and CSS style controls need to be used. (1) Preparation work First, in the HTML file

Using JavaScript functions to implement user login and permission verification With the development of the Internet, user login and permission verification have become essential functions for many websites and applications. In order to protect users' data security and access rights, we need to use some technologies and methods to verify the user's identity and restrict their access rights. As a widely used scripting language, JavaScript plays an important role in front-end development. We can use JavaScript functions to implement user login and permission verification functions

Using JavaScript functions to implement file upload and download With the development and popularization of the Internet, file upload and download have become one of the common functions in web applications. This article will introduce how to use JavaScript functions to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server through a web page. FileAPI is provided in HTML5 to handle the selection and upload of files. We can take advantage of Fi in FileAPI

Using JavaScript functions to achieve user interaction and dynamic effects With the development of modern web design, user interaction and dynamic effects have become the key to attracting users' attention. As a commonly used scripting language, JavaScript has powerful functions and flexible features, and can achieve a variety of user interactions and dynamic effects. This article will introduce some common JavaScript functions and give specific code examples. Changing element style (style) can be easily changed through JavaScript functions

Dynamically updated data visualization using JavaScript functions Data visualization is a very important part of the big data era. It can display data in an intuitive way and help people better understand and analyze the data. JavaScript, as a client-side scripting language, can achieve dynamic updates of data visualization through functions. This article will introduce how to use JavaScript functions to achieve this functionality and provide specific code examples. 1. Basics of data visualization before starting to write code
