patternjavascriptMinor
Basic JavaScript OOP model object
Viewed 0 times
javascriptobjectmodeloopbasic
Problem
I'm relatively new to JavaScript OOP and would like to know the best practice when doing a simple object such as a Model.
Here's a piece of code representing class named
What I'm asking for is:
Classic way to declare a class:
```
var obj = function() {
var attribut = "foo";
this.metho = function(parameter1, parametre2) {
alert("parameters: " + parameter1 + ", " + parameter2);
Here's a piece of code representing class named
Child:What I'm asking for is:
- The usage of this (and the weak reference I'm doing which I name self)
- The global shape of the code (methods in prototype etc)
- Declaring a class as a function, or with JSON semantics
var Child = function( json ) {
var self = this;
// Properties
this.id = 0;
this.firstname = "";
this.moneyCurrent = 0;
this.moneyDue = 0;
this.missionsPlay = 0;
this.missionsWait = 0;
this.missionsStop = 0;
this.missions = [];
this.initialize( json );
}
Child.prototype = {
initialize: function( json ) {
var self = this;
self.id = json.id_child;
self.firstname = json.firstname;
self.moneyCurrent = parseFloat(json.already_paid) + parseFloat(json.to_pay) - parseFloat(json.spent_money);
self.moneyDue = parseFloat(json.to_pay);
self.missionsPlay = json.nb_missions.in_progress;
self.missionsWait = json.nb_missions.waiting;
self.missionsStop = json.nb_missions.completed;
},
getMissions: function( type, callback ) {
var self = this;
session.requestServer( "parent_getChildMissionList", {
sid: session.sid,
id_child: self.id,
status: type ? type : "",
strict_status: false,
page: 0
}, function( data ){
var missions = [];
$(data.missionList).each(function(){
self.missions.push(new Mission(this));
missions.push(self.missions[self.missions.length-1]);
});
callback( missions );
});
},
};Classic way to declare a class:
```
var obj = function() {
var attribut = "foo";
this.metho = function(parameter1, parametre2) {
alert("parameters: " + parameter1 + ", " + parameter2);
Solution
For your questions:
Other than that,
-
The function to deal with returned missions seems a bit clumsy, I would either assign each new mission to a variable and push it to both arrays:
or, I would only
All in all, nice code, very maintainable and self explanatory.
- The usage of
this: In my mind, unless you are using closures, do not useself = this, so only keep it ingetMissions
- Functions in
prototype
- Function <> JSON, is an odd question. You want to use constructors, and those are functions.
Other than that,
- You are using lowerCamelCase, that is good. Though
firstname->firstName
initialize: function( json )does not really get a JSON string, instead it gets an object, so I would not name that parameterjson
-
The function to deal with returned missions seems a bit clumsy, I would either assign each new mission to a variable and push it to both arrays:
function( data ){
var missions = [], mission;
$(data.missionList).each(function(){
mission = new Mission(this)
self.missions.push( mission );
missions.push( mission );
});
callback( missions );
}or, I would only
push to missions and then concatenate missions into self.missionsfunction( data ){
var missions = [];
$(data.missionList).each(function(){
missions.push(new Mission(this));
});
self.missions = self.missions.concat( missions );
callback( missions );
}All in all, nice code, very maintainable and self explanatory.
Code Snippets
function( data ){
var missions = [], mission;
$(data.missionList).each(function(){
mission = new Mission(this)
self.missions.push( mission );
missions.push( mission );
});
callback( missions );
}function( data ){
var missions = [];
$(data.missionList).each(function(){
missions.push(new Mission(this));
});
self.missions = self.missions.concat( missions );
callback( missions );
}Context
StackExchange Code Review Q#46719, answer score: 7
Revisions (0)
No revisions yet.