Home Web Front-end JS Tutorial Eval and new funciton not the same thing_javascript技巧

Eval and new funciton not the same thing_javascript技巧

May 16, 2016 pm 05:45 PM
eval new

1、函数声明式

复制代码 代码如下:

function foo(){
//code
}

在JS中,函数也是对象,函数对象连接到Function.prototype( Function.prototype连接到Object.prototype)
2、函数字面量式
复制代码 代码如下:

var foo = function foo(){
//code
}

对象拥有一个连到原型对象的隐藏连接。对象字面量间生的对象连接到Object.prototype。 foo.__proto__ == Function.prototype
3、使用New的构造函数生成

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

每次执行都生成新的函数
网上的资料有很多介绍这三种模式的,前2种几乎是相同的,基于相同的词法作用域。

词法作用域:变量的作用域是在定义时决定而不是执行时决定,也就是说词法作用域取决于源码,通过静态分析就能确定,因此词法作用域也叫做静态作用域。 with和eval除外,所以只能说JS的作用域机制非常接近词法作用域(Lexical scope)。
突然感觉有点离题了,这篇文章其实是记录eval和New Function的区别,下面回归正题:

以前有人会说,new Function的方式是几乎与eval相等,今天我查了一下,确实是不同的东西,说这句话的人太不负责了。关于eval和new function,得到的结果都是一致的,都会叫你不要去使用它们。所以结论就是“不得不”才使用。

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.
从以上2点看出,eval的作用域是现行的作用域,而new Function是动态生成的,它的作用域始终都是window。并且,eval可以读到本地的变量,new Function则不能。
复制代码 代码如下:

function test() {
var a = 11;
eval('(a = 22)'); //如果是new Function('return (a = 22);')(); a的值是不会覆盖的。
alert(a); // alerts 22
}

所以一般eval只用于转换JSON对象,new Function也有特殊的用途,只是在不清楚的情况下还是少用为妙。

更多资料:邪恶的eval和new Function

这里作个备份:

代码:
复制代码 代码如下:

// 友善提醒:为了你的手指安全,请在Chrome下运行
'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);'))();
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does eval mean in python? What does eval mean in python? May 22, 2019 pm 03:18 PM

eval means "evaluation" and is a built-in function in Python that is used to execute a string expression and return the calculation result of the expression; that is, when assigning a variable, the representation on the right side of the equal sign is written in the format of a string, The return value is the result of this expression. Syntax "eval(expression[, globals[, locals]])".

How to disable eval in php under win How to disable eval in php under win Oct 31, 2022 am 09:33 AM

How to disable eval in PHP under win: 1. Download "PHP_diseval_extension"; 2. Find the PHP currently used by the server; 3. Open the PHP configuration file; 4. Add the "extension=diseval.so" code; 5. Restart the service.

A simple explanation of the usage of eval in python A simple explanation of the usage of eval in python Mar 25, 2024 pm 02:08 PM

In Python, the eval() function is used to execute a string expression and return its result. It takes a string containing an expression as a parameter and evaluates the expression. The eval() function is powerful, but it should be noted that it will execute any valid Python expression contained in the string, so you should avoid accepting external input strings when using it to prevent security vulnerabilities.

what is eval in python what is eval in python Aug 08, 2023 pm 05:07 PM

eval is a built-in function in python that is used to parse and execute strings as codes and return the execution results. Since the "eval" function can execute arbitrary Python code, you need to carefully consider security issues when using the "eval" function. If you pass an untrusted string to the "eval" function, it may lead to security issues such as code injection.

How to use Python eval function How to use Python eval function Jun 04, 2023 am 09:19 AM

Python’s eval() We can use the built-in Pythoneval()[1] to dynamically evaluate expressions from string-based or compiled code-based input. If we pass a string to eval(), then the function parses it, compiles it to bytecode[2], and evaluates it as a Python expression. But if we call eval() with a compiled code object, then the function only performs the calculation step, which is very convenient if we call eval() multiple times with the same input. Python's eval() is defined as follows. eval(expression[,globals[,locals]]) this function

How to use the new keyword in java How to use the new keyword in java May 03, 2023 pm 10:16 PM

1. Concept In the Java language, the "new" expression is responsible for creating an instance, in which the constructor is called to initialize the instance; the return value type of the constructor itself is void, not "the constructor returns the newly created Object reference", but the value of the new expression is a reference to the newly created object. 2. Purpose: Create an object of a new class. 3. Working mechanism: Allocate memory space for object members, and specify default values. Explicitly initialize member variables, perform construction method calculations, and return reference values. 4. Instance new operation often means opening up new memory in the memory. The memory space is allocated in the heap area in the memory. It is controlled by jvm and automatically manages the memory. Here we use the String class as an example. Pu

What is the difference between make and new in go language What is the difference between make and new in go language Jan 09, 2023 am 11:44 AM

Differences: 1. Make can only be used to allocate and initialize data of types slice, map, and chan; while new can allocate any type of data. 2. New allocation returns a pointer, which is the type "*Type"; while make returns a reference, which is Type. 3. The space allocated by new will be cleared; after make allocates the space, it will be initialized.

See all articles