TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • Syntax
  • Closure

Was this helpful?

  1. JavaScript

The "new Function" syntax

Syntax

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

// 有參數
let sum = new Function('a', 'b', 'return a + b');
alert( sum(1, 2) ); // 3

// 無參數
let sayHi = new Function('alert("Hello")');
sayHi(); // Hello

// 最大的不同是用字串建構一個函式,使用的廠景很特殊,像是從 server 取得函式程式碼。
let str = ... receive the code from a server dynamically ...
let func = new Function(str);
func();

Closure

// new 創建的函式 [[Environment]] 指向全域,因此得不到函式內部的變數。
function getFunc() {
  let value = "test";

  let func = new Function('alert(value)');

  return func;
}

getFunc()(); // error: value is not defined

// normal
function getFunc() {
  let value = "test";

  let func = function() { alert(value); };

  return func;
}

getFunc()(); // "test", from the Lexical Environment of getFunc

// 這樣的特性可以減少使用上的錯誤,假設寫腳本時不知道 new 函式的程式碼,執行時才能獲得程式碼,
// 我們可能會用 minifier 打包,這樣函式內部變數會被改變,當 new 的程式碼被傳進來時,已經
// 不知道他的變數是哪個內部變數了。

PreviousFunction object, NFENextScheduling: setTimeout and setInterval

Last updated 5 years ago

Was this helpful?