Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


JavaScript OOP - Classes & Objects
JavaScript OOP - Methods & Properties
JavaScript OOP - Getters & Setters
JavaScript OOP - Encapsulation
JavaScript OOP - Inheritance

 

JavaScript OOP - Classes and Objects

 

//Object instantiation

var obj0 = new SomeClass();
var obj1 = new Object();
var obj2 = {};
var img = new Image();

alert(obj0+", "+obj1+", "+obj2+", "+img);


// Classes / Objects

// Objects can be created in different ways.

//Class
function SomeClass0(){
    property = "Property 0";
    var property = "Property 1";
    this.property = "Property 2";
    
    method0 = function(){ }
    this.method1 = function(){ }
}

//Object
var SomeClass1 = new Object();
SomeClass1.constructor = function(){
    property = "Property 0";
    var property = "Property 1";
    this.property = "Property 2";
    
    method0 = function(){ }
    this.method1 = function(){ }
}

//Object
var SomeClass2 = {
    constructor: function(){
        property = "Property 0";
        var property = "Property 1";
        this.property = "Property 2";

        method0 = function(){ }
        this.method1 = function(){ }
    }
}

// Creating objects from classes/objects
var sc0 = new SomeClass0();
var sc1 = new SomeClass1.constructor();
var sc2 = new SomeClass2.constructor();

alert(sc0.property+", "+sc1.property+", "+sc2.property);


// Simple object
var simpleObject = {
    property0: "Property0",
    method0: function(){},
    method1: function(){}
}




Comments (0)