简体   繁体   English

JavaScript类的构造函数返回一个函数或一个对象怎么了?

[英]What's wrong with a JavaScript class whose constructor returns a function or an object

When I use new to instainciate an instance of a certain class, I got the actual instance. 当我使用new实例化某个类的实例时,我得到了实际的实例。 When the constructor function has a return value, the new sentence gives out the actual instance also. 当构造函数具有返回值时, new语句还将给出实际实例。 However, when the constructor returns itself, I can't get the instance. 但是,当构造函数返回自身时,我无法获取实例。 Instead I get the constructor. 相反,我得到了构造函数。 I wander what's wrong with this. 我徘徊了这是怎么回事。

Here are my test code fragment: 这是我的测试代码片段:

function foo() {
    this.x = 1;
    return foo;
}
console.log(new foo()); // prints the defination of foo

As we consider, in most situations, it makes no sence to return a function like this. 我们认为,在大多数情况下,返回这样的函数毫无意义。 However, why does JS has such a feature? 但是,为什么JS具有这种功能? Is there any consideration when designing JS? 设计JS时需要考虑什么? Or is it just a bug of JS? 还是仅仅是JS的错误?

If you return an object from a constructor, the result of the new ... expression will be the object you returned: 如果从构造函数返回对象,则new ...表达式的结果将是您返回的对象:

 function myWackyConstructor() { return new Date(); } var d = new myWackyConstructor(); console.log(d); 

If you return a primitive, the result will be the constructed object: 如果返回原语,则结果将是构造的对象:

 function myWackyConstructor() { this.gotAValue = true; return 3; } var v = new myWackyConstructor(); console.log(v.gotAValue); 

Typically, you should not return anything from a constructor: 通常,您不应从构造函数返回任何内容:

 function myNormalConstructor() { this.gotAValue = true; } var v = new myNormalConstructor(); console.log(v.gotAValue); 

The question is, why are you returning the constructor from itself? 问题是, 为什么要从自身返回构造函数?

Your code does raise an eyebrow. 您的代码确实引起了人们的注意。 It does not make sense to me why you would return a static class in your constructor. 对于我来说,为什么在构造函数中返回静态类并不有意义。

I think if you returned the actual instance it might make more sense to you but it isn't necessary. 我认为,如果您返回实际实例,则对您来说可能更有意义,但这不是必需的。

example

function foo() {
    this.x = 1;
    return this;
}

var aFooInstance = new foo();

console.log(aFooInstance); // prints a instance of foo

However, you might want to have private variables so you can return an object like so, in this example you can also pass in the data to the constructor. 但是,您可能希望拥有私有变量,以便可以像这样返回一个对象,在此示例中,您还可以将数据传递给构造函数。

function foo(x) {

    var _x = x;

    this.getX = function(){ return _x;}      
}

var aFooInstance = new foo(1);

console.log(aFooInstance.getX()); // prints 1

I would suggest reading more on simple class instantiation . 我建议阅读更多有关简单类实例化的内容

In JS, when we declare a function as a class and when we create an object of that class, that function gets called first. 在JS中,当我们将一个函数声明为一个类并创建该类的对象时,该函数将首先被调用。

Dont return from the function. 不要从函数返回。

The Javascript constructor does not need a return. Javascript构造函数不需要返回。 This will work:- 这将起作用:

function foo() {
    this.x = 1;
}

var myfoo = new foo();
console.log(myfoo.x);

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

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