Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

How to define your own JavaScript class

There are several ways to define a class in Javascript. I will be listing my favorite method below (JSON Method). It’s important to note that there are no real classes in JavaScript. Everything is considered an object. So below is our class definition:

<script type="text/javascript">
var Person = {
    age: "25",
    sex: "Male",
    location: "New York",
    getASL: function () {
        return 'Age: ' + this.age + ', Sex: ' + this.sex + ', Location: ' + this.location;
    }
};
</script>

So you can start using the class like this:

<script type="text/javascript">
Person.age = "29";
alert(Person.getASL());
</script>
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

7 Responses to “How to define your own JavaScript class”

  1. Comment #1 - Permalink
    Doug
    06/21/2009

    This was very helpful.

  2. Comment #2 - Permalink
    Joseph Montanez
    07/23/2009

    Actually thats an object. If you ever do new Person(), its ignored and your just copied the object. So what doesn’t work is if try to prototype Person so what every existing object of Person will also be extended with the new function / variable.

    so it should be:
    var Person = function() {
    this.age = “25″
    this.getASL = function() {
    // stuff here
    }
    }
    or
    function Person() {
    this.age = “25″
    //etc
    }
    Person.prototype.getASL() = function () {
    // stuff here
    }

    Its an anal comment but still something nice to know.

  3. Comment #3 - Permalink
    Richard
    07/28/2009

    @Joesph

    Thanks!

  4. Comment #4 - Permalink
    Pomax
    03/13/2010

    and how does one define a function with input? How would one, for instance, define a setAge(var age) function for Person, so we can say

    var person = new Person();
    person.setAge(23);
    alert(“person’s age: “+person.getAge());

  5. Comment #5 - Permalink
    M A Hossain Tonu
    05/24/2010

    If you interested on JSON style JavaScript class declaration talks in more detail…

    http://mahtonu.wordpress.com/2010/04/13/json-style-javascript-object-declaration/

  6. Comment #6 - Permalink
    M A Hossain Tonu
    05/24/2010

    Nice post :)

    Actually JSON objects are singleton objects by design, not by choice, since they have no constructor. I really like JSON :D

    So using the JSON you can define a class, while at the same time creating an instance (object) of that class, means that you can have only one single instance of this class at any time, you cannot instantiate more objects of the same class.

    More here:
    http://mahtonu.wordpress.com/2010/04/13/json-style-javascript-object-declaration/

  7. Comment #7 - Permalink
    Derrick
    07/24/2010

    Actually, Joseph Montanez, you never want to do (or suggest) what you’ve done in your first example.

    You are redefining the prototype methods each and every time a new instance of the class is instantiated.

    Have a look here:
    http://ejohn.org/blog/simple-class-instantiation/

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top