Special Values
Null,undefined,{},NaN,Infinity
Special Datatype
object,function,string,number
Typecase
parseInt(),parseFloat(),Boolean()
check datatype :
typeof()-return as string like 'number','object'
instanceof-[] instanceof Array=>true
isNaN(),isFinite()
String Functions:
length,indexOf(),substr(),slice(),split,trim(),search(REGULAR EXPR)
Create Object:
var obj1={first_name='manab',last_name='basu'}
delete properties from object:
delete user.first_name
to add properties in object :
user["first_name"]="Manab"
lenght Of Object
Object.keys(user).length
Array:
Create Array:var arr=[] or var arr=['a','b','c']
typeof arr=Object
Check : Array.isArray(arr)
Add Item: arr.push('a') or arr[4]='a' or arr.unshift('c')- add element at beginning
Delete Item-delete arr[3] or arr.pop()-pop item from the end,arr.shift()-Remove item from begening
Extract Element from array-arr.splice(START_INDEX,NO OF ELEMENT) & it reduce the array size based on NO OF ELEMENT
String to array:"A,b,c".split(',');
Array to string:[1,2,3].join(":");=>1:2:3
Sort:arr.sort()
Sort String of array(names):
names.sort(function(a, b){
var a1=a.toLowerCase(),b1=b.toLowerCase();
if(a1 < b1) return -1;
if(a1 > b1) return 1;
return 0;
})
Functions:
check arguments:write- console.log(arguments);- within function
Constraint
Ternary Operator: var x=today?"X":"Y"
prototype:The prototype property is initially an empty object, and can have members added to it - as you would any other object.
var myObject = function(name){
this.name = name;
return this;
};
console.log(typeof myObject.prototype); // object
myObject.prototype.getName = function(){//adding a new member getName() to the class myObject
return this.name;
};
or
function Shape(){
};
Shape.prototype.X=0;
Shape.prototype.Y=0;
Shape.prototype.move=function(x,y){
this.X=x;
this.Y=y;
}
Shape.prototype.distance=function(){
return Math.sqrt(this.X*this.X+this.Y*this.Y);
}
//call
var s=new Shape();
s.move(10,10);
console.log(s.distance());
Inheritance:
function Square(){}
Square.prototype=new Shape();
Square.prototype.__proto__=Shape().prototype;
Square.prototype.width=0;
Square.prototype.area=function(){
return this.width*this.width;
}
var sq=new Square();
sq.move(5,5);
sq.width=15;
console.log(sq.distance());
console.log(sq.area());
Error Handling:
function test(){
throw new Error("Bad Error");
}
//Calling Function
try{
test();
}
catch(e){
console.log(e.message);
}
Global: global object
global.name="Manab";
global["name"];
Convert DATETIME TO TIMESPAN IN C#
13 years ago