Object Oriented JavaScript
Amit Jain
Front-end engineer, CSS, HTML , JavaScript junkie and web lover. Senior UI developer @ iXiGO.com
var a = 5, b = a;
b = 7;
console.log(a,b);
var a = {val:5}, b = a;
b.val = 7;
console.log(a.val,b.val);
var obj = new Object();
var objRef = obj;
obj.oneProperty = true;
obj.oneProperty === objRef.oneProperty
var items = new Array( "one", "two", "three");
var itemsRef = items;
items.push( "four" );
items.length == itemsRef.length
var foo = "test"; // global scope
if ( true ) { var foo = "new test"; }
alert( foo == "new test" );
function test() {
var foo = "old test"; }
test(); alert( foo == "new test" );
function test() { foo = "test2"; }
// Call the function to set the value of foo
test(); alert( window.foo == "test2" );
var baz;
(function() { var foo = 10; var bar = 2; baz = function() {
return foo * bar; };
})();
baz();
// baz can access foo and bar, even though it is executed outside of the function
function foo() { var a= 10;
function bar() { a*= 2; return a; }
return bar; }
var baz = foo(); // baz is now a reference to function bar.
baz(); // returns 20.
baz(); // returns 40.
baz(); // returns 80.
var blat = foo(); // blat is another reference to bar.
blat(); // returns 20, because anew copy of a foo is being used.The context of a function can be explicitly set using function.apply() and function.call()
( function eatCake(){
alert("So delicious and moist");
} )();var ixigo = new Object();
ixigo.name = "ixigo";
ixigo.species = “travel";
ixigo.hello = function() { alert(“fly high"); }
function Pet(name, species, hello){
this.name = name;
this.species = species;
this.hello = hello;
this.sayHello = function() { alert(this.hello);}
}
var rufus = new Pet("Rufus", "cat", "miaow");
rufus.sayHello();
alert(rufus.constructor.toString());
Implementation is shared using the prototype property of the constructor function
function Pet(name, species, hello){
this.name = name;
this.species = species;
this.hello = hello; }
Pet.prototype.sayHello = function() { alert(this.hello); }
var rufus = new Pet("Rufus", "cat", "miaow");
rufus.sayHello();
function Person( name ) { this.name = name; }
Person.prototype.getName = function() { return this.name; };
function User( name, password ) {
// Notice that this does not support graceful overloading/inheritance
// e.g. being able to call the super class constructor
this.name = name; this.password = password;
};// The User object inherits all of the Person object's methods
User.prototype = new Person();
User.prototype.getPassword = function() {
return this.password; };
function Person( name ) { this.name = name; }
Person.prototype.getName = function() { return this.name; };
function User( name, password ) {
Person.call(this, name); // Call the superclass's constructor in the scope of this.
this.password = password;
};
// The User object inherits all of the Person object's methods
User.prototype = new Person();
User.prototype.constructor = User; // Set the constructor attribute to User
User.prototype.getPassword = function() {
return this.password;
};

Questions & Answers!