2013-06-26 36 views
0

我是新的面向对象的JavaScript。试图制作一个对象构造函数。 这里是我的代码javascript:当声明一个原型方法:错误是无效的左侧赋值

function Collection() { 
    this.ports = build_ports_collection(); 
    this.all_things = build_things_collection(); 
    this.added_things = function() { 
     this.added_things.total_added = 0; 

     var temp = this.all_things; 
     temp.splice($(sth).val(), 1); 
     this.added_things.all = temp; 
    }; 
}; 
Collection.ports.prototype.reload = function() { 
    Collection.ports = build_ports_collection(); 
}; 
Collection.all_things.prototype.reload = function() { 
    Collection.all_things = build_things_collection(); 
}; 
Collection.added_things.all.prototype.reload() = function() { 
    var temp = Collection.all_things; 
    temp.splice($(sth).val(), 1); 
    Collection.added_things.all = temp; 
}; 
Collection.added_things.prototype.add_things = function() { 
    this.added_things.total_added++; 
    add_things(); 
}; 
Collection.added_things.prototype.remove_things = function() { 
    this.added_things.total_added--; 
    remove_things(); 
}; 

我收到错误的行Collection.added_things.all.prototype.reload()= ....

netbeans的报告:无效的左手侧的分配。

这里我的目的是绑定的方法重载()来Collection.added_things.all,这样它会收集

我缺少点什么的所有实例之间共享?

回答

0

不知道这是否是您正在寻找的答案,但想到这一点。

Collection.added_things.all.prototype.reload() = function() { 
    var temp = Collection.all_things; 
    temp.splice($(sth).val(), 1); 
    Collection.added_things.all = temp; 
}; 

在那里,你要分配东西的Collection.added_things.all原型,而

this.added_things = function() { 
    this.added_things.total_added = 0; 

    var temp = this.all_things; 
    temp.splice($(sth).val(), 1); 
    this.added_things.all = temp; 
}; 

是第一个宣布.allCollection.added_things - 基本上,你正在试图将值分配给属性/变量/指针不存在,直到added_things第一次被调用。

+0

我明白了, 非常感谢这么快的回应:) – minhajul

相关问题