Master_script_maker
11-05-2008, 07:36 PM
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:
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:
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.
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:
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:
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.