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的更多相关文章

  1. 《理解 ES6》阅读整理:函数(Functions)(三)Function Constructor & Spread Operator

    增强的Function构造函数(Increased Capabilities of the Function Constructor) 在Javascript中Function构造函数可以让你创建一个 ...

  2. 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】

    大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看 以下代码均已折叠,点击“+“即可打开 一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作 #includ ...

  3. Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...

  4. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  5. js in depth: arrow function & prototype & this & constructor

    js in depth: arrow function & prototype & this & constructor https://developer.mozilla.o ...

  6. 理解callback function in javascript

    以下内容主要摘自[1,2] (1)In javascript, functions are first-class objects, which means functions can be used ...

  7. JS原型的问题Object和Function到底是什么关系

    var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...

  8. 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel

    在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...

  9. js中Object.__proto__===Function.prototype

    参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...

随机推荐

  1. LintCode_69 二叉树前序遍历

    题目 给出一棵二叉树,返回其节点值的前序遍历. 和中序遍历基本相同 C++代码 vector<int> preorderTraversal(TreeNode *root) { // wri ...

  2. 大数据技术之Zookeeper

    第1章 Zookeeper入门 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. 1.2 特点 1.3 数据结构 1.4 应用场景 提供的服务包括:统 ...

  3. PLAY2.6-SCALA(五) Action的组合、范围的设置以及错误的处理

    一.自定义action 从一个日志装饰器的例子开始 1.在invokeBlock方法中实现 import play.api.mvc._ class LoggingAction @Inject() (p ...

  4. 洛谷 1463[SDOI2005] 反素数ant

    题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...

  5. 利用IDEA构建springboot应用-配置文件

    application.properties配置文件(不建议采用这种配置) 配置文件采用:application.yml文件会更简便,要带空格 属性配置与类中取值 添加bean属性配置到一个类里面,采 ...

  6. HDU 5572 An Easy Physics Problem【计算几何】

    计算几何的题做的真是少之又少. 之前wa以为是精度问题,后来发现是情况没有考虑全... 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意: ...

  7. python if 选择结构

  8. Firefox浏览器怎么安装adobe flash player插件

    https://jingyan.baidu.com/article/0a52e3f435d171bf62ed7237.html 有些朋友在使用Firefox火狐浏览器,但是火狐浏览器安装以后是没有fl ...

  9. 洛谷P2455 [SDOI2006]线性方程组

    高斯消元模板 要求输出解的情况(无穷解/无解) 1. 之前写的丑陋代码 #include <iostream> #include <cstdio> #include <c ...

  10. Knative Eventing 中如何实现 Registry 事件注册机制

    摘要: 在最新的 Knative Eventing 0.6 版本中新增了 Registry 特性, 为什么要增加这个特性, 该特性是如何实现的.针对这些问题,希望通过本篇文章给出答案. 背景 作为事件 ...