Here: http://www.phpied.com/3-ways-to-define-a-javascript-class/ are described 3 ways to define a "class" in javascript. I chose in my code to use the 3rd method:
var apple = new function(){
//code here
}
I use this construction as a way to separate code in namespaces, I mean I have more variable as apple in the same js file, and each one may contain functions with the same name.
var customer = new function(){
this.URL = "//customer/";
this.json = {};
this.getData = function(id){
this.prepareForm();
this.json = requestData(this.URL); //requestData deffined in another place
}
this.save = function(){
//code
}
this.validate = function(){
//code
}
this.prepareForm = function(){
//code
}
// more code here
}
var employee = new function(){
//code here, something like the customer code
}
var partner = new function(){
//code here, something like the customer code
}
Now, I noticed that sometimes this.URL is undefined. The this.prepareForm() function exists, is deffined and runs on context, the variable doesn't exist and I need to call it customer.URL instead. But this is only sometimes, not all the time, and I didn't understand why and when happened.
Any suggestion? Why this happens?
this.validate(){doesn't look right? Did you meanthis.validate = function() {?