PDA

View Full Version : How to develop a javascript class



veerreddy
09-01-2008, 06:35 PM
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

jlizarraga
09-01-2008, 07:08 PM
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. :D

Twey
09-01-2008, 11:26 PM
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:
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:
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:
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):
function OriginCircle(radius) {
this.radius = radius;
}

OriginCircle.prototype = new Point(0, 0);

var ocirc = new OriginCircle(5);
ocirc.x; // 0
ocirc.y; // 0