Javascript 对象作为引用的新实例?

Javascript 对象作为引用的新实例?,javascript,object,reference,prototype,instance,Javascript,Object,Reference,Prototype,Instance,我在创建对象的新实例时遇到问题 使用下面的代码,我希望每个元素都有自己的随机值(正在发生) 但是我还希望this.element值包含在对象的每个实例中,但是每次对象的任何实例中的值发生更改时,它都会在所有实例中更新 var Instance = function(element) { this.$element = $(element); this.subInstance.parent = this; } Instance.prototype = { subInst

我在创建对象的新实例时遇到问题

使用下面的代码,我希望每个元素都有自己的随机值(正在发生)

但是我还希望
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();
});​
现在我知道我可以使用
this.subInstance={…
将子实例移出原型并放入构造函数,但这似乎是错误的,为什么
this.$element
不包含在对象的每个实例中


js两个示例中的一个:

这看起来可能是错误的,但事实并非如此。如果从构造函数创建的每个对象都需要使用唯一的
子实例
,则需要为每个实例创建一个新实例。在
原型
上,它将被共享

但是,您可以做的一件事是使用
对象。create
创建一个从原型
子实例继承而来的新实例。然后您可以从一些重用中获益,每个实例都可以修改自己的对象

var Instance = function(element) {
    this.$element = $(element);
    this.subInstance = Object.create(this.subInstance);
    this.subInstance.parent = this;
}
现在有人可能会说,
子实例
仍然不应该出现在
原型
上,而应该是IIFE中的局部变量。我倾向于同意这一点

下面是一个例子:

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;
})();

请注意,函数的
由函数的调用方式设置,而不是由函数的声明或初始化方式设置(使用绑定除外)

在代码中,您有:

> Instance.prototype = {
>     subInstance: {
它将对象分配给具有作为对象的
子实例属性的
Instance.prototype

其次是:

>         init: function() {
>             var $this = this;
>             this.$element = this.parent.$element;
该方法称为:

> instance.subInstance.init();

因此
这个
init
方法中总是引用同一个对象(即
Instance.prototype.subInstance
),因此分配给
this。$element
不断替换值。

只有函数具有
this
值,而不是普通对象。函数具有
this
的值由调用设置。
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());