Javascript: OOP in javascript using Objects and prototypes

HTML5 PoweredJavascript does not support real oop and classes but you can fake it and get the similar effects using Objects and prototypes.

First, to create a “Car” class with some properties, make a function Car and set internal properties using “this” reference:

function Car(s, w) {
  this.speed = s;
  this.weight = w;

To add methods to your Car class, use the prototype to that class and assign then anonymous functions:

Car.prototype.go() {
 console.log("car speed is : " + this.speed);

Car.prototype.stop() {
  console.log("car weight is : " + this.weight);

Here is an example of your Car class being used:

var car = new Car(65, 2000);

If you are coming from actionscript or any other modern programming language, you are probably laughing, but this is how it goes and it is the future of the web.

Author: mark

Creative technologist

Leave a Reply

Your email address will not be published. Required fields are marked *