为什么我的JavaScript实例返回相同的?

时间:2014-12-22 13:21:58

标签: javascript oop

我试图创建一个名为constructorOne的函数变量的两个实例(constructorTwomyObjectConstructor)。出于某种原因,当我调用getProperty时,我得到两个实例的相同结果。

//this is one other way of creating a Constructor function
var myObjectConstructor = function(){
    this.myProperty = '';

    init = function(str) {
       this.myProperty = str;
    },

    getProperty = function() {
       return this.myProperty;
    }     

    return {
        init: function () {
            return init.apply(self, arguments);
        },
        getProperty: function () {
            return getProperty.apply(self, arguments);
        }
    }
}

//instantiate our Constructor
var constructorOne = new myObjectConstructor();

//change myProperty of the first instance
constructorOne.init('this is property one');

//instantiate a second instance of our Constructor
var constructorTwo = new myObjectConstructor();

constructorTwo.init('this is property two');

constructorOne.getProperty()constructorTwo.getProperty()都会提醒" 这是属性二":

alert(constructorOne.getProperty()); //this will alert 'this is property two'
alert(constructorTwo.getProperty()); //this will still alert 'this is property two'

这是demo

问题是,为什么constructorOne.getProperty()没有返回'这是属性#'?

2 个答案:

答案 0 :(得分:6)

selfundefined所以在init函数中,thiswindow,所以你总是在修改同一个对象的属性。

您似乎正在对模块模式和标准JavaScript构造函数进行一些奇怪的混合。我强烈建议选择其中一种方法并坚持下去。



function MyObjectConstructor(){
    this.myProperty = '';
}

MyObjectConstructor.prototype.init = function(str) {
       this.myProperty = str;
};
MyObjectConstructor.prototype.getProperty = function() {
       return this.myProperty;
};
 
//instantiate our Constructor
var constructorOne = new MyObjectConstructor();
 
//change myProperty of the first instance
constructorOne.init('this is property one');
 
//instantiate a second instance of our Constructor
var constructorTwo = new MyObjectConstructor();
 
constructorTwo.init('this is property two');

//alert current myProperty of constructorOne instance
alert(constructorOne.getProperty()); 

 //alert current myProperty of constructorTwo instance
alert(constructorTwo.getProperty()); 






function myModule (){
  var myProperty = '';
  return {
    init: function (str) {
       myProperty = str;
    },
    getProperty: function () {
      return myProperty;
    }
  };  
}

//instantiate our Constructor
var constructorOne = myModule();
 
//change myProperty of the first instance
constructorOne.init('this is property one');
 
//instantiate a second instance of our Constructor
var constructorTwo = myModule();
 
constructorTwo.init('this is property two');

//alert current myProperty of constructorOne instance
alert(constructorOne.getProperty()); 

 //alert current myProperty of constructorTwo instance
alert(constructorTwo.getProperty());




答案 1 :(得分:1)

更好的方法是使用适当的原型:

function MyObjectConstructor() {
    this.property = '';
}

MyObjectConstructor.prototype.init = function(newProperty) {
    this.property = newProperty;
};

MyObjectConstructor.prototype.getProperty = function() {
    return this.property;
};