简体   繁体   English

对象的新实例充当参考?

[英]new instance of object acting as reference?

I am having a problem creating new instances of an object. 我在创建对象的新实例时遇到问题。

Using the below code I was expecting each element to have it's own random value (which is happening). 使用以下代码,我期望每个元素都有其自己的随机值(正在发生)。

But then I was also expecting the this.element value to be contained to each instance of the object, but instead every time the value is changed in any instance of the object it is updating in all of them. 但是后来我也期望this.element值包含在对象的每个实例中,但是每次在对象的任何实例中更改该值时,它都将在所有对象中进行更新。

var Instance = function(element) {
    this.$element = $(element);
    this.subInstance.parent = this;
}

Instance.prototype = {

    subInstance: {

        parent: null,
        $element: null,

        init: function() {
            var $this = this;
            this.$element = this.parent.$element;

            //test for what this.$element refers to in init function
            var random = Math.random();
            $('.element', this.$element).text('Updated ' + random);

            //test for what this.$element refers to in an event handler
            $('.element', this.$element).on('click', function(e) {
                $this.$element.css('background-color', '#f00');
            });
        }
    }
}
$('div.instance').each(function(i, o) {
    var instance = new Instance(o);
    instance.subInstance.init();
});​

Now I know I could move subInstance out of the prototype and into the constructor using this.subInstance = {... but that seems wrong, why is this.$element not contained to each instance of the object? 现在我知道我可以使用this.subInstance = {...将subInstance移出原型并移入构造函数this.subInstance = {...但这似乎是错误的,为什么this.$element不包含在对象的每个实例中?

JSFiddle of both examples: http://jsfiddle.net/q7zAg/ 两个示例的JSFiddle: http//jsfiddle.net/q7zAg/

It may seem wrong, but it's not. 看起来似乎是错误的,但事实并非如此。 If each object created from the constructor needs to work with a unique subInstance , you'll need to create a new one for each individual instance. 如果从构造函数创建的每个对象都需要使用唯一的subInstance ,则需要为每个单独的实例创建一个新的对象。 On the prototype it will be shared. prototype ,它将被共享。

However , one thing you could do would be to use Object.create to create a new instance that inherits from the prototyped subInstance . 但是 ,您可以做的一件事是使用Object.create创建一个从原型subInstance继承的新实例。 Then you get the benefit of some reuse, and each instance can modify its own object. 然后,您将获得一些重用的好处,并且每个实例都可以修改自己的对象。

var Instance = function(element) {
    this.$element = $(element);
    this.subInstance = Object.create(this.subInstance);
    this.subInstance.parent = this;
}

Now some may argue that the subInstance still shouldn't be on the prototype , but rather should be a local variable in an IIFE. 现在有人可能会争辩说, subInstance仍然不应该在prototype ,而应该是IIFE中的局部变量。 I would tend to agree with this. 我倾向于同意这一点。

Here's an example: 这是一个例子:

var Instance = (function() {
    var _subInstance = {
        parent: null,
        $element: null,
        init: function() {
            // ...
        }
    };

    var Instance = function(element) {
        this.$element = $(element);
        this.subInstance = Object.create(_subInstance);
        this.subInstance.parent = this;
    };

       // other prototyped props
    Instance.prototype.foo = "bar";

    return Instance;
})();

Note that a function's this is set by how the function is called, not how it's declared or initialised (other than using bind ). 请注意,函数的this取决于函数的调用方式,而不是函数的声明或初始化方式(使用bind除外)。

In your code you have: 在您的代码中,您具有:

> Instance.prototype = {
>     subInstance: {

which assigns an object to Instance.prototype that has a subInstance property that is an Object. 它将一个对象分配给具有subInstance属性(即Object)的Instance.prototype

Then there is: 然后是:

>         init: function() {
>             var $this = this;
>             this.$element = this.parent.$element;

The method is called as: 该方法称为:

> instance.subInstance.init();

So this within the init method always references the same object (ie Instance.prototype.subInstance ), so assignments to this.$element keep replacing the value. 所以, this在中init方法总是引用相同的对象(即Instance.prototype.subInstance ),所以分配到this.$element不断替换值。

var Obj = 
{
    internal:null,

    __init:function(data){
        this.internal = data;
        return this
    },
    get:function(){
        return this.internal;
    },
    init:function(){
        var args = arguments,instance = function(){
             return this.__init.apply(this,args);
        };
        instance.prototype = this;
        return new instance();
    }
}

console.log(Obj.init(123).get());
console.log(Obj.get());
console.log(Obj.init(321).get());
console.log(Obj.get());

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

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