var obj = {};
Adding a property to an object
var obj = { name: 'Jason', age: 20 };
var car2 = new Car("Nissan", "300ZX", 1992);
function myFunction(prop) {
return something ;
};
var myFunction = function (prop) { };
var myFunction = function newFn(prop) { };
function myFunction( prop1, prop2){
var val = arguments[0];
arguments[0] = 'Some Value'; };
function Phone( model , manufacturer) {
this.model = model ;
this.manufacturer = manufacturer ;
this.displayModel = function(){
alert(this.model);
}
this.displayManu = function(){
alert(this.manufacturer);
}
};
var myXperia = new Phone('Xperia','SONY');
myXperia.constructor === Phone // true
var o = {};
o.constructor === Object; // true var a = []; a.constructor === Array; // true
function Phone( ) {
var propertyName = 'some value';
var functionName = function(){ ... };
};
Classname.propertyName = 'some value' ;
Phone.color = 'Black' ;
var staticObj = {
method1 : function (){ alert('method1');} ,
method2 : function (){ alert('method2');} } staticObj.method2();
function isPrime( num ) {
if ( isPrime.cache[ num ] != null )
return isPrime.cache[ num ];
var prime = num != 1; // Everything but 1 can be prime
for ( var i = 2; i < num; i++ ) {
if ( num % i == 0 ) {
prime = false;
break;
}
}
isPrime.cache[ num ] = prime ;
return prime;
}
isPrime.cache = {};
isPrime(5);
function Car(){
this.noWheels = 4;
}
Car.prototype.color = 'black';
var c = new Car();
function changeColor(color){
this.color = color;
}
changeColor.call(c,'white');
alert(c.color);
function Car(){
}
Car.prototype.wheels = 4;
Car.prototype.drive = function (){
alert('drive');
};
function Car(){
this.drive = function(){
alert('unable to drive right now');
}
}
Car.prototype.drive = function (){
alert('drive');
};
var c = new Car();
c.drive(); // what will be the alert ?
function Car(){
this.noWheels = 4;
}
Car.prototype.color = 'black';
var c = new Car();
c.hasOwnProperty('color'); // false
c.hasOwnProperty('noWheels'); // true
c.__proto__.hasOwnProperty('color') // true
function Person(){}
Person.prototype.dance = function( ){ alert('dancer'); };
function Dancer(){}
Dancer.prototype = new Person();
var dancer = new Dancer();
dancer.dance();
alert(dancer instanceof Dancer); // true
alert(dancer instanceof Person); // true
alert(dancer.constructor === Dancer) // false
alert(dancer.constructor === Person) // true
Dancer.prototype.constructor = Dancer;
alert(dancer.constructor === Dancer) // true
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price); // call super using call and passing ‘this’
this.category = 'food';
}
Food.prototype = new Product();
var cheese = new Food('feta', 5);
alert(cheese.name);
function MyError(message) {
this.name = "MyError";
this.message = message || "Default Message";
}
MyError.prototype = new Error();
MyError.prototype.constructor = MyError;
function Person(){}
Person.prototype.dance = function( ){ alert('dance'); };
function Dancer(){}
Dancer.prototype = new Person();
Dancer.prototype.dance = function(){ alert('dance better')};
var dancer = new Dancer();
dancer.dance(); // alert dance better