简体   繁体   English

javascript 初始化一个没有 new 关键字的对象

[英]javascript init an object without the new keyword

What's the difference between:有什么区别:

function Foo(){} var foo1 = Foo();

and

var foo2 = new Foo()

As far as I tested, foo1 gives nothing.据我测试, foo1 什么也没提供。 typeof foo1 is undefined while with new it's ok as expected. typeof foo1 未定义,而使用 new 则可以按预期进行。

What's the reason the that without the new keyword I get the undefined result?没有 new 关键字我得到未定义结果的原因是什么?

This is because function Foo(){} returns undefined implicitly, ie if a function does not have an explicit return statement, the compiler/VM will implicitly add it.这是因为function Foo(){}隐式返回undefined ,即如果函数没有显式返回语句,编译器/VM 将隐式添加它。 However, when you use the new keyword, you instantiate an object from the constructor function.但是,当您使用new关键字时,您会从构造函数中实例化一个对象。

See this answer for an exact explanation for what the new keyword does.有关new关键字的作用的确切解释,请参见此答案

Here's what's basically happening when you are using new , it:这是您使用new时发生的基本情况,它:

  1. Creates a new object, let it be o .创建一个新对象,让它成为o
  2. Sets the prototype link of o to [constructor].prototype .o的原型链接设置为[constructor].prototype
  3. Executes the [constructor] with o as the context object ( this ).使用o作为上下文对象 ( this ) 执行[constructor]
  4. Returns o unless the [consructor] returns a non-primitive value.除非[consructor]返回非原始值,否则返回o In that case, that non-primitive value is returned instead of o .在这种情况下,将返回该非原始值而不是o (Added these precisions as suggested by @Esailija). (按照@Esailija 的建议添加了这些精度)。

When you are not using new , nothing of this happens and the context object is window , unless the function was invoked on another object.当您不使用new时,不会发生这种情况并且上下文对象是window ,除非该函数是在另一个对象上调用的。 In this case, the context object would be that object.在这种情况下,上下文对象就是那个对象。

Eg例如

function A() {}
A(); //this points to window

var o = { A: A };

o.A(); //this points to o

Every function can be a constructor in JavaScript, however you must use the new keyword in order to get the expected result.每个function都可以是 JavaScript 中的构造函数,但是您必须使用new关键字才能获得预期的结果。

To avoid mistakes, some people will design their function so that forgetting the new keyword will not be harmful, like below, however there are better alternatives to detect these mistakes, like using code analysis tools like JSHint .为了避免错误,有些人会设计他们的函数,以便忘记new关键字不会有害,如下所示,但是有更好的替代方法来检测这些错误,比如使用JSHint之类的代码分析工具。

Eg例如

function Foo(args) {
    if (!(this instanceof Foo)) {
        return new Foo(args);
    }
    //initialization code
}

The code contained a safeguard to bail if the new keyword wasn't used.如果不使用新关键字,该代码包含一个保释措施。 Which is a good idea because if the new keyword is omitted, the context will be a window.这是一个好主意,因为如果省略 new 关键字,上下文将是一个窗口。

function MyConstructor(){
  console.log(this.toString());
}

var instance1 = MyConstructor();
var instance2 = new MyConstructor();

This outputs:这输出:

"[object Window]"
"[object Object]"

So, in your example it shows undefined as a window which is bind automatically has no foo1 method, in the second case, new instantiate/declare an object has method foo2因此,在您的示例中,它显示未定义为自动绑定的窗口没有 foo1 方法,在第二种情况下,新实例化/声明一个对象具有方法 foo2

Try this尝试这个

function Foo(){} var foo1 = Foo();

instead of代替

function Foo(){} var foo1 = foo();

JavaScript is case sensitive language JavaScript 是区分大小写的语言

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM