The Function() Constructor
Functions are usually defined using the function keyword, either in the form of a function definition statement or a function literal expression. But functions can also be defined with the Function() constructor. For example:
var f = new Function("x", "y", "return x*y;");
This line of code creates a new function that is more or less equivalent to a function defined with the familiar syntax:
var f = function(x,y) { return x*y; }
The Function() constructor expects any number of string arguments. The last argument is the text of the function body; it can contain arbitrary JavaScript statements, separated from each other by semicolons. All other arguments to the constructor are string that specify the parameters names for the function. If you are defining a function that takes no arguments, you simply pass a single string -- the function body -- to the constructor.
Notice that the Function() constructor is not passed any argument that specifies a name for the function it creates. Like function literals, the Function() constructor creates anonymous functions.
There are a few points that are important to understand about the Function() constructor:
The Function() construnctor allows JavaScript functions to be dynamically created and compiled at runtime.
The Function() constructor parses the function body and creates a new function object each time it is called. If the call to the constructor appears within a loop or within a frequently called function, this process can be inefficient. By contrast, nested function and function definition expressions that appear within loops are not recompiled each time they are encountered.
At last, very important point about Function() constructor is that the functions it creates do not use the lexical scoping; instead, they are always compiled as if they were top-level functions, as the following code demonstrates:
var scope = "global";
function constructFunction() {
var scope = "local";
return new Function("return scope"); //Does not capture the local scope
}
// This line return "global" because the function returned by the
// Function() constructor does not use the local scope.
constructFunction()(); // => "global"
The Function() constructor is best thought of as a globally-scoped version of eval() that defines new variables and functions in its own private scope. You should rarely need to use this constructor in your code.
The Function() Constructor的更多相关文章
- 《理解 ES6》阅读整理:函数(Functions)(三)Function Constructor & Spread Operator
增强的Function构造函数(Increased Capabilities of the Function Constructor) 在Javascript中Function构造函数可以让你创建一个 ...
- 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...
- Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...
- 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍
一 Prototype.__proto__与Object.Function关系介绍 Function.Object:Js自带的函数对象. prototype,每一个 ...
- js in depth: arrow function & prototype & this & constructor
js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...
- 理解callback function in javascript
以下内容主要摘自[1,2] (1)In javascript, functions are first-class objects, which means functions can be used ...
- JS原型的问题Object和Function到底是什么关系
var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...
- 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel
在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...
- js中Object.__proto__===Function.prototype
参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...
随机推荐
- 移动项目到centos中运行报错:failed to open stream: Permission denied
这是文件夹没有读写权限的错误: (注意:TP5.0权限给runtime文件夹就行了,官方文档在安装tp5的方法中有介绍到权限问题) 在需要赋予权限的文件夹的前一级输入: chmod -R 文件夹名字
- R是用于统计分析、绘图的语言和操作环境
R是一套完整的数据处理.计算和制图软件系统.其功能包括:数据存储和处理系统:数组运算工具(其向量.矩阵运算方面功能尤其强大):完整连贯的统计分析工具:优秀的统计制图功能:简便而强大的编程语言:可操纵数 ...
- Adapter小练习
Aapter的继承关系图: Android中Adapter的是数据和视图之间的桥梁,数据在adapter中做处理,然后显示到视图上面. 一.ArrayAdapter适配器 java代码: import ...
- js表格上下移动添加删除
html部分 <div onclick='fn()'>加</div> <table width="250" border="1" ...
- 洛谷P4126 [AHOI2009]最小割
题目:洛谷P4126 [AHOI2009]最小割 思路: 结论题 在残余网络上跑tarjan求出所有SCC,记id[u]为点u所在SCC的编号.显然有id[s]!=id[t](否则s到t有通路,能继续 ...
- 笔记:字体大小的几种不同的格式px,em,rem
px px像素(Pixel),相对长度单位,像素px是相对于显示器屏幕分辨率而言的.(引自CSS2.0手册) 譬如,Windows的用户所使用的分辨率一般是96像素/英寸. 而MAC的用户所使用的分辨 ...
- oracle表复杂查询--创建数据库实例
n 创建数据库有两种方法: 1)通过oracle提供的向导工具 2)我们可以用手工步骤直接创建 但我们创建完一个新的数据库实例后,在服务中就会有两个新的服务创建,这时,你根据实际需要去启动相应的数据 ...
- 说说iOS与内存管理(上)
http://www.cocoachina.com/ios/20150625/12234.html 说起内存管理,看似老生常谈,而真正掌握内存管理的核心其实并不简单.ARC/MRR以及“谁分配谁就负责 ...
- IoT SaaS加速器——助力阿尔茨海默病人护理
场景介绍 阿尔茨海默病,是导致中老年人认知功能障碍的最常见疾病之一,是发生在老年期及老年前期的一种原发性退行性脑病.据估计,全世界痴呆症患者数量为4700万,到2030年将达到7500万人.痴呆症患者 ...
- uva 11375 Matches (递推)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...