SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru CompiledMonkey's Avatar
    Join Date
    Sep 2002
    Location
    Richmond, VA
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you build "classes" in JavaScript?

    I've generally built a JavaScript file for each "class" I want to build. So, each class would look something like:

    Person.js
    Code:
    function Person()
    {
        var self = this;
    
        // variables
        this.XmlRequestPerson;
    
        // methods
        this.loginPerson = loginPerson;
    
        function loginPerson(userName, password)
        {
            // do some stuff!
        }
    }
    Having a class like this gives me the opportunity to do something like:

    Code:
    var person = new Person();
    person.loginPerson("sup", "dude");
    Are there better ways to do this? It just seems like the "var self = this" thing is stupid, because inside the Person.js file I have to reference everything local by "self" instead of "this". Thoughts??

  2. #2
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the OO way of coding, usseful when you want to make sure the variables used will not be messed with by other code that other people might add on your pages.

  3. #3
    SitePoint Guru CompiledMonkey's Avatar
    Join Date
    Sep 2002
    Location
    Richmond, VA
    Posts
    975
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any other ways, maybe better ways, to do this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •