简体   繁体   English

JavaScript新的Function表达式和新的Function构造函数

[英]JavaScript new Function expression and new Function constructor

My question is regarding the resulting object from using new function(){ ... } over new Function(); 我的问题是关于结果对象使用new function(){ ... }不是new Function();

My understanding so far/assumption 到目前为止我的理解/假设

  • When we create a function using Function() or new Function() we get a object configured as a function (it's internal slots indicate a function type object) 当我们使用Function()或new Function()创建一个函数时,我们得到一个配置为函数的对象(它的内部插槽表示一个函数类型对象)
  • With or without the new operator, the Function() constructor returns a new function object 无论有没有new运算符,Function()构造函数都会返回一个新的函数对象
  • Using a function expression returns a function object which internally uses the Function() constructor 使用函数表达式返回一个函数对象,该函数对象在内部使用Function()构造函数
  • A function expression provides optimization with parsing the function body 函数表达式提供了解析函数体的优化

My question 我的问题

Following my assumption above, why does new Function(); 按照我上面的假设,为什么new Function(); and new function(){ ... } return different things? new function(){ ... }返回不同的东西?

The first returns a function object, but the latter returns a standard object. 第一个返回一个函数对象,但后者返回一个标准对象。 Given that a function expression under the hood uses the Function() constructor, why does the latter not behave the same as new Function(); 鉴于引擎盖下的函数表达式使用Function()构造函数,为什么后者的行为与new Function(); ?

Using new function(){ ...} I would expect a function object, not a standard object. 使用new function(){ ...}我希望有一个函数对象,而不是标准对象。

The following lines are essentially the same: 以下几行基本相同:

function foo(...) {...}
var foo = function (...) {...};
var foo = new Function (...);

They all declare a new function (which is an object, that is an instance of Function , which inherits the Object prototype, ...) in the current scope which can be accessed via the foo variable. 它们都在当前作用域中声明了一个新函数(它是一个对象,它是一个Function的实例,它继承了Object原型,......),可以通过foo变量访问它。

The new keyword instantiates an object from a function which allows you to create something similar to a class instance in a standard OOP language. new关键字从函数中实例化一个对象,该函数允许您使用标准OOP语言创建类似于类实例的内容。

Continuing the example from above: 继续上面的例子:

var bar = new foo(...)

would instantiate an instance of the foo function in the current scope which could be accessed via the bar variable. 将在当前作用域中实例化foo函数的实例,该实例可以通过bar变量访问。

why does new Function(); 为什么new Function(); and new function(){ ... } return different things? new function(){ ... }返回不同的东西?


I'm going to rephrase the example slightly. 我稍微会重复一下这个例子。 Instead of: 代替:

new Function()
//and
new function(){ ... }

I'm going to use 我打算用

new Function()
//and
var foo = function () { ... };
new foo();

The reason that new Function() returns a different thing than new foo() is entirely because Function is a different function from foo . new Function()返回与new foo()不同的东西的原因完全是因为Function是与foo不同的函数。

I expect that you're not confused by the fact that new Object() returns a different object with different features than new Array() . 我希望您不会因为new Object()返回具有与new Array()不同的功能的不同对象这一事实而感到困惑。 The exact same thing is happening here. 完全相同的事情发生在这里。

Given that a function expression under the hood uses the Function() constructor, why does the latter not behave the same as new Function(); 鉴于引擎盖下的函数表达式使用Function()构造函数,为什么后者的行为与new Function(); ?

under the hood the later expression is essentially: 后面的表达基本上是:

new (new Function ( ... ))

new Function(...) returns a new function , while new function() {...} returns a new Object . new Function(...)返回一个新函数 ,而new function(){...}返回一个新Object

Basically, Function is just a way to evaluate a piece of code. 基本上,Function只是评估一段代码的一种方式。

Objects are a way, to store data in javascript. 对象是一种在javascript中存储数据的方式。

Side note: you can technically do 旁注:你可以在技术上做到

new (new Function(...))

wich returns a new object. 返回一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript-具有new关键字的函数构造函数 - Javascript - function constructor with new keyword javascript新的对象构造函数替代正确吗? - javascript new object constructor function alternative proper? javascript / typescript 中的构造函数 function 和新 object - constructor function and new object in javascript / typescript 使用构造函数创建新对象并在javascript中调用函数 - Making a new object using a constructor function and calling a function in javascript 新的super.constructor是JavaScript中的有效表达式吗? - Is new super.constructor a valid expression in JavaScript? 由new构造函数创建的函数对象是否在javascript中被视为可变对象? - Is function object created by `new` constructor treated as mutable object in javascript? 什么时候在JavaScript的构造函数和类中创建新对象? - When are new objects created in JavaScript's constructor function vs in class? 有效的JavaScript:使您的构造函数功能与新的无关 - Effective JavaScript: Making your Constructor Function new-agnostic 如何在函数中获取新对象的构造函数的名称? (JavaScript)的 - How to get the name of the new object's constructor in the function? (Javascript) 用new实例化对象时,Javascript调用一次构造函数 - Javascript invoke constructor function once when object instantiated with new
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM