JavaScript - 对象原型中的Getter/Setter不起作用.Getter返回最后一个元素

Abh*_*nav 1 javascript prototype last-modified getter-setter

我是JavaScript的新手.在使用给定的javascript时,我期望输出值为"123"; "测试1"; "456"和"test2",因为有两个实例.

但返回的值是"456";"test2"; "456"和"test2".

如果我更改setter以处理"this"(目前在代码中已经出现),问题就解决了.我无法理解为什么protype对象表现得很差.

我不想用这个声明变量,因为我希望变量是私有的,只能通过getter/setters方法访问.

<html>
<head>
<script type="text/javascript">

function Test(){
    var name;
    var id;


    Test.prototype.setId = function(newId){
        id = newId; 
    }

    //this.id = function(newId){
    Test.prototype.getId = function(newId){
        return  id;
    }


    Test.prototype.setName = function(newName){
        name = newName; 
    }

    //this.getName = function(newGuid){
    Test.prototype.getName = function(newGuid){
        return name;    
    }


}



function callme(){
    var instance1 = new Test();
    instance1.setId("123");
    instance1.setName("test1");
    var instance2 = new Test();
    instance2.setId("456");
    instance2.setName("test2");
    alert(instance1.getId());
    alert(instance1.getName());
    alert(instance2.getId());
    alert(instance2.getName());
}


</script>
</head>

<body>
<button onclick='callme()'> Test </button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

根据下面给出的Matt的建议,我已经改变了我的代码.但代码不适用于继承.我修改了代码如下.当前实现创建派生类型的对象并尝试调用基类的方法.

<html>
<head>
<script type="text/javascript">

(function () {
    var stores = [];
    var counter = 0;
    Test = function () {
        this.storeId = stores.length;
        stores.push({});
    }

    Test.prototype.setGuid = function (newId) {
        stores[this.storeId].id = newId;
    }
    Test.prototype.getGuid = function (newId) {
        return stores[this.storeId].id;
    }
    Test.prototype.setName = function (newName) {
        stores[this.storeId].name = newName;
    }
    Test.prototype.getName = function (newGuid) {
        return stores[this.storeId].name;
    }
})();

Derived.prototype = new Test();
Derived.prototype.constructor = Derived

function Derived(){
    Test();//call to base class
    //some additional method
}


function callme(){
    var instance1 = new Derived();
    instance1.setGuid("123");
    instance1.setName("test1");

    var instance2 = new Derived();
    instance2.setGuid("456");
    instance2.setName("test2");

    alert(instance1.getName());
    alert(instance1.getGuid());

    alert(instance2.getName());
    alert(instance2.getGuid());



}


</script>
</head>

<body>
<button onclick='callme()'> Test </button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

其次,stores当变量被清除(超出范围)时,我不确定从变量中清除内存.

Den*_*ret 5

您的房产是全球性的.您必须使用它this来引用实例的属性.

像这样修复你的代码:

function Test(){
    this.name =''; // this only to show that the property exist,
                   //  it doesn't change the behavior as you set it later
    this.id='';
}

Test.prototype.setId = function(newId){
    this.id = newId; 
}

Test.prototype.getId = function(){
    return this.id;
}


Test.prototype.setName = function(newName){
    this.name = newName; 
}

Test.prototype.getName = function(){
    return this.name;    
}
Run Code Online (Sandbox Code Playgroud)

我解决了一些其他问题,其中包括你不应该声明参数的事实,getX因为你不需要它.