javascript - Trying to add a bit logic to game -


i have fight game , want add bit more interaction, kinds of abilities. way this? constructor , list of abilities or i've missed more simple method this?

var abilityconstructor = function(name, desc, icon, target, param, rate, duration) {     this.name = name;         // name of ability     this.desc = desc;         // ability's description     this.icon = icon;         // ability's icon     this.target = target;     // if self usage - 0, if enemy - 1     this.param = param;       // parameter influenced (health - 0, damage - 1, speed - 2, missing rate - 3)     this.rate = rate;         // factor dealing (ability's strength)      this.duration = duration; // spells' duration (current round - 1, 2 rounds - 2, 3 rounds - 3) }  // list of available rates abilities var lowrate = 0.1; var midrate = 0.25; var highrate = 0.5;  var testability = new abilityconstructor('health reduction', 'reduces health of opponent 2 rounds', 'icon_path_here', 1, 0, midrate, 2); 

well, first of convention it's better have constructor functions capitalized since helps identity them such , dont append constructor word either.

also, it's preference, when start have lot of parameters, prefer take single data or options object argument.

finally, if copying on configurations, looping on configuration keys.

function ability(data) {     (var k in data) {         this[k] = data;     } } 

however, might not able generalize much, instead of looping on manually copy values doing.

function ability(data) {     this.name = data.name;     //... } 

here's how call constructor:

var testability = new ability({     name: 'health reduction',     ... }); 

edit:

the advantage of construct not have remember parameters order , it's simplifying life when arguments might optionnal. instead of passing undefined, can omit key in data object. pattern used in pretty every javascript libraries, including jquery. however, sacrificing on performance since data object has created every time. advise try pattern , fallback on initial solution if performance real issue.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -