English 中文(简体)
面向目标的联合材料——没有全球变量?
原标题:Object-Oriented JS - without global variables?

I am taking an object-oriented approach to building a Javascript app. For starters, I am creating a Game object and a Timer object.

Here s my current implementation: The Timer object has the following methods:

setTimerValue()  
start()  
stop()  
getTimeLeft()  
timeExpired()

The Game object has the following methods:

reset()
run()
...

Timer.start() uses window.setInterval() to start the countdown. When the countdown gets to zero, it calls Timer.timeExpired().

<代码>timer.timeExpired(>>宣布,游戏已经结束,应当打电话Game.reset(Game.run(<>,以重新开始游戏。 但是,如果时间客人不了解任何关于猎物的内容,如何做到这一点?

动物目标是否需要成为一种全球性的变数,才能发挥作用?

此外,目前,该动物物体的特性之一为时标。 这使得狩猎者反对使用时间方法。 但是,不清楚时间方法如何能把猎物称作猎物。

I have a nagging suspicion that I am creating too many dependencies between the objects and could use some suggestions on how to architect this better.

Thanks everyone.

最佳回答

Use an event driven approach or the observer design pattern. Javascript has one of the flexible event driven implementation from my experience. So you have the Timer object which doesn t really have to know about any other objects. When you call Timer.start(), the object should emit a custom event, like "timerStarted", likewise for the other methods:

Timer.stop() - > emits "timerStoped"
Timer.timeExpired() -> emits "timerExpired"

对于时方提出的每一项活动,都应当有一个活动手,当核心活动发生时,将叫作活动手。

如果你使用舱位,处理事件就应当小:

var $ = jQuery.noConflict();
var timer = new Timer();
var game  = new Game();

$(timer).bind( timerStarted , function(e) {
    game.doSomething();
});

时间:开始()

start: function() {
    $(this).trigger( timerStarted );
}

Another approach is using callbacks: If you want something to be done when the timer starts, you could pass a lambda function to the start method of the timer:

// timer method
start: function(callback) {
    if(typeof(callback) ==  function ) {
        callback();
    }
}

// main logic
var timer = new Timer();
var game  = new Game();

timer.start(function() {
    game.doSomething();
});

我希望你们会得到ide。

问题回答

暂无回答




相关问题
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.

热门标签