Helpful Information
 
 
Category: Coding tips & tutorials threads
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:

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.

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.

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.

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

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

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.










privacy (GDPR)