English 中文(简体)
如何设计这一架构?
原标题:How to design this architecture?

我有3个班级:

function A(data){
    this.data = data || [];
}    
A.prototype.remove = function(index){
    // remove index from this.data;
};
function B(data){
    this.data = data || [];
}  
B.prototype.add = function(x){
    this.data.push(x);
};
B.prototype.update = function(index, x){
    this.data[index] = x;
};
function C(data){  
    this.data = data || [];
}
C.prototype.add = function(x){
    this.data.push(x);
};
var a = new A(xx);
var b = new B(xx);
var c = new C(xx);

There relations are:
if I call a.remove(i) to remove an element, b will add it to its data.
if I call b.update(i, x), c will add x to its data.
How could I design this with smallest coupling?

ps: 这只是表明情况的一个实例,请不注意初始化等。

问题回答

我要补充A类和B类的反馈。

function A(data, callback) {
  this.data = data || [];
  this.callback = callback || function() {};
}

A.prototype.remove = function(index) {
  // remove index from this.data;
  this.callback(this.data[index]);
}
...
var b = new B(xx);
var a = new A(xx, function(e) {b.add(e);});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签