[英]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 到目前为止我的理解/假设
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();
andnew 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 asnew 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.