Importance of good javascript convention

I've been writing my js13k submission whenever i have had chance over the last few days, and it's made me think more about the way i write javascript.

I haven't has much experience in my working life with javascript, i've dabbled here and there (moreso in my current role). I've read multiple design patterns that can be implemented using javascript, but i still tend to write javascript in an oop style, asd i just find it easier to follow. I always think separation of concern is the most important thing to start of with; I find that if you don't start separating things out at the earliest stages, the code you write becomes more and more unmanageable as you write it.

The way i like to write javascript - using IOC where possible: This is what i call a View Controller pattern, it separates the view code (if using jQuery etc.) and the controller. It makes testing of the code very easy, you only really need to assert that methods were called on the controller, then test the view separately.

I have tried to apply this pattern to my js13k game, but since I haven't had much experience with game code (I spend 99% of my time writing business logic etc.) it kind of follows, but not really strictly. Plus normally I would write test driven, thats probably where the game went wrong!!
1:  function View(){  
2:    return {  
3:      getSomeValue: function(){  
4:        return $("idOfElement").val();  
5:      },  
6:      setSomeValue: function(value){  
7:        $("idOfElement").val(value);  
8:      }  
9:    };  
10:  }  
11:  function Controller(view){  
12:    Controller.prototype = this;  
13:    Controller.prototype.view = view;  
14:  }  
15:  Controller.prototype.Initialise = function(){  
16:    //constructor code  
17:    var controller = Controller.prototype;  
18:    controller.SomeValueChanged();  
19:  }  
20:  Controller.prototype.SomeValueChanged = function(){  
21:    var view = Controller.prototype.view;  
22:    view.setSomeValue("value");  
23:  }  
24:  domReady(function(){  
25:    var view = new View();  
26:    var controller = new Controller(view);  
27:    controller.Initialise();  
28:  });  

0 mentions:

Post a Comment