Results 1 to 3 of 3

Thread: How to develop a javascript class

  1. #1
    Join Date
    Sep 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to develop a javascript class

    When i read articals on the net, it stated that java script also follows oop concepts.

    As per as i know we can create objects in javascript like calender or string using the existing java script types.

    My question is can we create a new type in javascript, like employee, here employee contains the properties as id, name,sal and a method as perform.

    Once we create this type, we can create objects of employee.

    Is this flexibility available in javascript or not, if not why javascript developer's says that javascript is follow oop concepts.

    Regards
    Veer

  2. #2
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    I just learned this stuff like two days ago!:

    function employee(id,name,sal) {
    this.id = id;
    this.name = name;
    this.sal = sal;
    this.perform = function() {
    alert("I said work, " + this.name + "! *cracks whip*")
    }
    }

    var someEmployee = new employee("1234XY9","John",50000);

    someEmployee.perform(); // alerts "I said work, John! *cracks whip*"

    I learned this from the following links:

    http://www.javascriptkit.com/javatutors/object.shtml
    http://www.javascriptkit.com/javatutors/oopjs.shtml

    This is my first time being able to answer someone's question.
    Last edited by jlizarraga; 09-01-2008 at 07:10 PM. Reason: typo

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

    Default

    Not entirely accurately, I'm afraid.

    It is true that Javascript is OO. However, it does not use class-based OO of the type you might expect in Java or C#. Instead, it uses a form of prototypical OO: an OO paradigm is simpler than classical OO and can also be considerably more flexible and powerful (unfortunately, it has been poorly-implemented in Javascript). There is no distinction between 'classes' and their instances; there are only objects, which inherit from one another, rather than some abstract form. One can create an object by doing:
    Code:
    var point = {
      'x' : 2,
      'y : 5
    };
    If you have a somewhat abstract object that you feel you will need to create multiple times, you can create a function to construct that object:
    Code:
    function create_point(x, y) {
      return {
        'x' : x,
        'y' : y
      };
    }
    
    var pt = create_point(2, 5);
    Javascript provides some syntactic sugar for this, known as 'constructor functions'. These are special functions that begin with a capital letter (by convention) and are invoked using the 'new' keyword. The 'new' keyword handles some magic setup stuff: it will pass the object being constructed to the constructor function as 'this', and return that object when the constructor function is done, ignoring the return value of said constructor function:
    Code:
    function Point(x, y) {
      this.x = x;
      this.y = y;
    }
    
    var pt = new Point(2, 5);
    There's one other thing that 'new' does, that makes it not-quite-just-sugar: it applies a 'prototype' to the new object. This object will not be directly accessible from the instance, but is one on which properties will be looked up if they are not found on the new object. This is the biggest error in the Javascript implementation, since the prototypes of objects are not copied over to the new object, thus limiting their usefulness (as demonstrated by the following contrivance):
    Code:
    function OriginCircle(radius) {
      this.radius = radius;
    }
    
    OriginCircle.prototype = new Point(0, 0);
    
    var ocirc = new OriginCircle(5);
    ocirc.x; // 0
    ocirc.y; // 0
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ý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
  •