Evalandnewfuncitonnotthesamething_javascript tips-js tutorial

Source: Internet
Author: User
Some people may say that the newFunction method is almost the same as eval. Today I checked it and it is really different. The people who say this sentence are too irresponsible. The results of eval and newfunction are the same, and you are told not to use them. So the conclusion is that "I have to" use 1. Function declarative

The Code is as follows:


Function foo (){
// Code
}


In JS, functions are also objects. Function objects are connected to Function. prototype (Function. prototype is connected to Object. prototype)
2. Function literal expression

The Code is as follows:


Var foo = function foo (){
// Code
}


The object has a hidden connection to the prototype object. Objects generated between objects are connected to Object. prototype. Foo. _ proto _ = Function. prototype
3. Use the New constructor to generate

New Function ([arg1 [, arg2 [,... argN],] functionBody );

Each execution generates a new function.
There are many materials on the Internet that describe these three modes. The first two are almost the same, based on the same lexical scope.

Lexical scope: the scope of a variable is determined during definition rather than execution. That is to say, the lexical scope depends on the source code and can be determined through static analysis. Therefore, the lexical scope is also called static scope. Except with and eval, we can only say that the JS scope mechanism is very close to the Lexical scope ).
I suddenly felt a little digress. This article is actually a record of the differences between eval and New functions. Next we will return to the question:

Some people may say that the new Function method is almost the same as eval. Today I checked it and it is really different. The people who say this sentence are too irresponsible. The results of eval and new functions are the same, and you are told not to use them. Therefore, the conclusion is that "I have to" use it.

Eval () evaluates a string as a JavaScript expression within the current execution scope and can access local variables.
New Function () parses the JavaScript code stored in a string into a function object, which can then be called. It cannot access local variables because the code runs in a separate scope.
From the above two points, we can see that the eval scope is the current scope, while the new Function is dynamically generated, and its scope is always window. In addition, eval can read local variables, but not new functions.

The Code is as follows:


Function test (){
Var a = 11;
Eval ('(a = 22)'); // if it is a new Function ('Return (a = 22); ') (); the value of a will not be overwritten.
Alert (a); // alerts 22
}


Therefore, eval is generally only used to convert JSON objects, and new functions have special purposes, but it is better to use fewer functions without knowing them.

More information: Evil eval and new Function

Here is a backup:

Code:

The Code is as follows:


// Friendly reminder: Please run in Chrome for your finger safety
'Alert ("hello") '. replace (/. +/, eval );
'Alert ("hello") '. replace (/. +/, function (m) {new Function (m )();});

Var I = 0; eval (new Array (101). join ('alert (++ I );'));
Var I = 0; new Function (new Array (101). join ('alert (++ I );'))();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.