Results 1 to 6 of 6

Thread: Javascript Objects and Classes

  1. #1
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default Javascript Objects and Classes

    Javascript Classes and Objects (a constantly improving guide)

    Hi everyone! I will try to explain objects and classes, and their advantages/disadvantages.

    1) Classes

    1.1) Explanation

    Classes are instructions for creating objects. Everything in the class will be copied to the object adopting it.

    1.2) Methods

    In javascript, classes are created much like functions. Lets start off with a simple class:
    Code:
    function simple_class() {
    	this.yes=false;
    	this.no=true;
    }
    var object=new simple_class();
    Now if you were to access object.yes, you would get the boolean value false.Lets look at a more advanced example:
    Code:
    function class2(a) {
    	this.yes=true;
    	this.no=false;
    	this.yandn = function() {
    		alert(this.yes);
    		alert(this.no);
    	}
    	this.age=a;
    	this.oldclass=new simple_class();
    }
    var newer = new class2(10);
    newer.prototype.older = new simple_class();
    var proto=new class2(12);
    proto.prototype = object;
    Now there are many things going on here. First I'll explain the class. In the class there are five children: yes, no, yandn, age, and old_class. yes and no both contain boolean values, yandn is a function, age is a variable passed through during the making, and old_class is an instance of the simple_class class.
    Var newer is an instance of class2 and therefore has all of it's properties, but we assign the newer object another property named older, which is an instance of the simple_class class (we could just as well assigned a string as a value or anything else), this is possible because of the prototype constructor. It allows the addition of properties to instances of classes.
    Var proto is also an instance of class2, but here we say proto.protoype = object;. How is this possible, and what does it do? this takes properties of object, and copies them over to proto, overwriting if necessary.

    1.3) Advantages

    The advantages of using classes over not using them:
    a) If you needed to create multiple objects of the same format. For example you had to create an object for each of 30 workers. If you create a workers class, it would be a lot easier.
    b) that is the only example i can think of right now. feel free to post suggestions

    1.4) Disadvantages

    The disadvantages of using classes over not using them:
    a)If there is a function in a class, it will be recreated. This is useful sometimes, but if the function doesn't need to be remade, take it out of the class.

    I'm currently working on the objects section. If you feel something needs to be added or corrected, please say so.
    Last edited by Master_script_maker; 11-08-2008 at 10:14 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Now there are many things going on here. First I'll explain the class. In the class there are three children: yes, no, yandn, b, and old_class
    No, there are 5 children and b is not included there. It's age.

    Anyway, nice explanation you got here. Well said.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There are no classes in Javascript. Javascript's OO is prototypical. There are only objects, although there are also functions (known as 'constructors', and conventionally given a capitalised identifier) that can help to create many instances of particularly useful objects. new is a little bit magic — and where Javascript's OO messes up, because thanks to new it's impossible to use pure prototypical/duck-typed OO.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franais | entiendo espaol | ti t hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Master_script_maker
    I'm currently working on the objects section. If you feel something needs to be added or corrected, please say so.
    Could you explain literal notation of objects? It makes them easier to read and understand.

    -magicyte

  5. #5
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Thanks guys.

    @rangana: thanks i changed that.

    @Twey: I know there are no real classes in javascript. i'm just trying to make it less confusing for inexperienced users.

    @magicyte: Ill try, but right now i have a lot of work from school. i might not start back on the objects section for another 2 weeks
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're only going to make things more complicated. Prototypical OO is actually considerably simpler than classical OO. It might seem simpler to someone with prior experience in languages with classical OO, but eventually they're going to run into a wall when they find some behaviour that just doesn't make sense in classical terms. It will also encourage poor programming style.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franais | entiendo espaol | ti t hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •