0

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?

6
  • 1
    If you're passing that "prepareForm" function as something like an event handler, it loses its relationship to the object. Commented Jan 23, 2014 at 16:22
  • this.validate(){ doesn't look right? Did you mean this.validate = function() { ? Commented Jan 23, 2014 at 16:23
  • Also note that that blog post is really old by JavaScript best-practice standards. Commented Jan 23, 2014 at 16:23
  • Edited. I wrote the cod directly on stackoverflow and I didn't saw that I didn't define well the functions. The point is that the functions are ok, my issue is with the URL variable, witch sometimes is undefined and I don't understand why! Commented Jan 23, 2014 at 16:39
  • Pointy: do you have any recommendation for a newer article about this? Commented Jan 23, 2014 at 16:59

2 Answers 2

1

You have some syntax errors in your code and as @Pointy has pointed out, the article is really old-fashioned. I really think you have to change the way you create Javascript classes, you better use prototype to define your class methods, this is ,IMHO, the better way to do what you want:

var Customer = function Customer(){
    this.URL = "//customer/";
    this.json = {};
};
Customer.prototype.getData = function(id){
    this.prepareForm();
    this.josn = requestData(this.URL);
};
Customer.prototype.save = function()(){
    //code
};
Customer.prototype.validate = function()(){
    //code
};
Customer.prototype.prepareForm = function()(){
    //code
};
var customer = new Customer();
//you can here call your methods like:
customer.validate();
  • And about your specific problem regarding this.URL being undefined, this problem happens only if you call the function with a different context, it even can be passing it as a callback or handler, based on your codes I guess you are passing the getData function as an callback to a ajax call, if it is so you better create an anonymous function and call the customer.getData() in it.
Sign up to request clarification or add additional context in comments.

Comments

0

this.json and this.josn = requestData(this.URL) are different. cannot be the answer but surely is not the same variable. take a look.

this.json = {};
this.getData = function(id){
    this.prepareForm();
    this.josn = requestData(this.URL);  //requestData deffined in another place

3 Comments

Why not? I don't understand
is misspelled look it one is this.json and the other this.JOSN
just a speling error, because I wrote all the code directly on the browser window. the real code is correct and still sometimes that variable is undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.