简体   繁体   English

您能帮我决定这个JavaScript设计问题吗?

[英]Can you help me decide on this JavaScript design problem?

I have a code as follow in making an object: 我在制作对象时有以下代码:

var myObject = {
    Init: function() {
        // config anything here to initiate this object.
    };
}

Where myObject.Init() performs like a constructor of a class. 其中myObject.Init()的执行类似于类的构造函数。 I use this Init() to generalize the object to be initiated in a following factory pattern: 我使用此Init()来概括以下工厂模式中要初始化的对象:

window[myObjectName].Init();

myObjectName is whatever name of the object to create. myObjectName是要创建的对象的任何名称。

Then again I have a 2nd choice to create an object, which is much more easier for me to actually turn the Init() into a some sort of a constructor like the code below: 然后再次有第二个选择来创建对象,这对我来说实际上更容易将Init()转换为某种构造函数,如下面的代码:

var myObject = function() {
    // config anything here to initiate this object.
}

With the code above, I can simply create an object like the following: 使用上面的代码,我可以简单地创建如下对象:

window[myObjectName]();

and practically reduces myObject code on having Init() attached to it (and every other object I would do in the factory). 并实际上减少了附加了Init()的myObject代码(以及我将在工厂中执行的所有其他对象)。

However the 2nd choice comes with a disadvantage I discovered, where I can't use this inside the object, that I actually have to explicitly use myObject to call things inside. 但是第二种选择带有一个缺点,我发现了一个缺点,即我不能在对象内部使用this ,实际上我必须显式地使用myObject在内部调用事物。 For example: 例如:

var myObject = function() {
    myObject.doSomething(); // instead of this.doSomething();
}

Which concerns me a little on variable/object reference (please correct me if I'm wrong). 这在变量/对象引用上让我有些担心(如果我错了,请纠正我)。

Any help on these choices' advantages and disadvantages? 这些选择的优缺点有什么帮助? which do you prefer on both of these choices or can you suggest a better solution? 您在这两个选项中都更喜欢哪个?或者您可以提出更好的解决方案?

Thanks. 谢谢。

Why not use a constructor function? 为什么不使用构造函数? Both approaches that you suggested requires the object to be created and stored before it can be initialised. 您建议的两种方法都需要先创建并存储对象,然后才能对其进行初始化。 You can do like this: 您可以这样:

function MyObject() {
  this.x = 1;
  this.y = 2;
}

You use the new keyword to create the objects: 您使用new关键字创建对象:

window[myObjectName] = new MyObject();

Another advantage with this is that you can use the prototype of the function to add methods to the objects that are created: 这样做的另一个好处是,您可以使用该函数的原型向创建的对象添加方法:

MyObject.prototype = {

  doSomething: function() {
    // do something
  }

  doMore: function() {
    // do more
  }

};

There are a couple of different ways to create class structures in js. 有两种不同的方法可以在js中创建类结构。 My personal preference, though is for something like your second example. 我个人的偏爱虽然是针对您的第二个示例。

var MyClass = function() {
    // define local variables
    var x = 0;
    function doSomething()
    {
       return ++x;
    }
    function doSomethingTwice()
    { 
        doSomething(); 
        doSomething(); 
    }

    // basically an export section where variables are assigned "public names"
    this.doSomething = doSomething;
    this.doSomethingTwice = doSomethingTwice;
    // careful though, this next line WON'T WORK!
    this.x = x; // because this is an assignment by value, this.x will be 0 
                // no matter how many times you `doSomething`
};
var inst = new MyClass(); // the `new` keyword is imperative here
                          // otherwise `this` won't work right.

Arly, in my uderstanding, you should definitely be able to use this.doSomething() . Arly,根据我的经验,您肯定应该能够使用this.doSomething() What is the error you're getting at that line of code? 您在该行代码中遇到的错误是什么?

The second way is actually the preferred one. 第二种方法实际上是首选方法。 this in JavaScript does not always refer to the current object. JavaScript中的this并不总是引用当前对象。 You usually keep a reference around to your object so that you don't have to rely on this . 通常,您对对象保持引用,这样就不必依赖this

Even when you write your code with the .Init function I as a caller can override your this by doing a 即使您使用.Init函数编写代码,作为调用者,我也可以通过执行

window[myObjectName].Init.call(mynewthis);

In JavaScript the caller decides on what this will refer to. 在JavaScript调用者决定什么this会参考。

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

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