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. Eclipse Jobs 和后台进程

    Eclipse后台进程 1.主线程(Main thread) 一个Eclipse客户端只能跑一个进程,但却可以创建很多线程. 在Eclipse框架中,会用一个单线程去运行所有的代码指令,这个线程执行客 ...

  2. img标签src不给路径就会出现边框

    <img/>在src加载失败或没有给的,浏览器会自动给img加上边框. 如下图这样: 产品觉得影响美观,一定要pass掉. 原码是这样: .ctn{ position: relative; ...

  3. python自定义函数和内置函数

    函数 1.定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 先定义,后使用 1.2分类 系统函数 自定义函数 1.3语法: def functionname(parameter ...

  4. MSSQL → 01:SQLServer 2008概述及安装

    据库的发展史 在人类诞生以来,就有记录数据的需求,在远古时代就有了结绳记事的故事,而随着科技的进步,我们记录数据的方式也发生了天翻地覆的变化,从效率低.规模小.不能适应信息高速发展的需要的手工或者简单 ...

  5. python学习之旅1-2(基础知识)

    三,python基础初识. 1.运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确定后 写入代码pytho ...

  6. 【数论】如何证明gcd/exgcd

    我恨数论 因为打这篇的时候以为a|b是a是b的倍数,但是懒得改了,索性定义 a|b 为 a是b的倍数 咳咳,那么进入正题,如何证明gcd,也就是 gcd(a,b) = gcd(b,a%b)? 首先,设 ...

  7. JS BOM 窗口中的使用

    1,弹框的种类 2,打开一个新页面 子窗口操作父窗口,打开新窗口显示内容open("opener.html","haha","width=300,he ...

  8. 集合--Collection接口详解&&迭代器

    /* * 集合的顶层接口--Collection接口 * 添加 * add() * addAll() * * 遍历集合 * iterator() 迭代器 * * 删除 * remove() * rem ...

  9. BZOJ5259/洛谷P4747: [Cerc2017]区间

    BZOJ5259/洛谷P4747: [Cerc2017]区间 2019.8.5 [HZOI]NOIP模拟测试13 C.优美序列 思维好题,然而当成NOIP模拟题↑真的好吗... 洛谷和BZOJ都有,就 ...

  10. 命令行的形式运行php

    转自:http://www.cnblogs.com/myjavawork/articles/1869205.html 注意:在安装php时需要将php 的安装目录加到环境变量 PATH 中 (右击我的 ...