Saturday, July 2, 2016

Array and Object Creation - JavaScript


var arr =[1,2,3];

var arr = new Array(1,2,3);

var arr = new Array(); //empty Array

arr.push(10);
arr.pop();

var obj = new Object();
obj.test ="hello";

var obj= { "test" : "hello"};


function myFunction() {
    alert('It works!');
}
var name = 'myFunction';
window[name].call();


var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: function(i) {
        return this.firstName + " " + this.lastName+i;
    }
};
document.getElementById("demo").innerHTML = myObject.fullName(1);
//John Doe1



//Passing value via constructor
function myFunction(arg1, arg2) {
    this.firstName = arg1;
    this.lastName  = arg2;
}
alert(JSON.stringify(new myFunction("John","Doe")))
var x = new myFunction("John","Doe")
//{"firstName":"John","lastName":"Doe"}

alert(JSON.stringify(new myFunction())) //{}

alert(JSON.stringify(new myFunction("John"))) //{"firstName":"John"}


//private method
var test = function(i){
alert(i);
}
//public method
this.test = function(i){
alert(i);
}
test("123");


var x = { foo : 1};
var output = (function(){
    delete x.foo;
    return x.foo;
  })();
alert(x.foo); //undefined
delete function used to delete the object properties, not a variable..


var Employee = {
  company: 'xyz'
}
var emp1 = Object.create(Employee);
//delete emp1 .company; //delete not working since its a referanced property.
alert(emp1 .company);

var foo = function(){ return 12; };
var foo = function bar(){ return 12; };
alert(foo());

function bar(){ this.y=12; return this.x=12; };
var dog = new bar();   // new return this referance .. eventhough it return anything
//alert(JSON.stringify(dog));
//alert(Object.keys(dog))
for(key in dog){}

function greeter(name, age) {
return name + " says howdy!! He is " + age + " years old";
}
// Generate the message
var message = greeter("James", 23);
//James says howdy!! He is 23 years old//



a.x=10;
a["x"] = 10;

isNaN
TypeOf
Undefined
this
alert
confirm
prompt
null
==
===
.value
.innerHTML


for (var i = 0; i < 5; i++) {
  var btn = document.createElement('button');
  btn.appendChild(document.createTextNode('Button ' + i));
  btn.addEventListener('click', function(){ alert(i); });
  document.body.appendChild(btn);
}

A callback function is executed after the current effect is 100% finished.
$("button").click(function(){
    $("p").hide("slow", function(){
        alert("The paragraph is now hidden");
    });
});

Number()
parseInt()
parseFloat()

var n1 = Number(“Hello world!”); //NaN
var n2 = Number(“”);             //0
var n3 = Number(“000010”);       //10
var n4 = Number(true);           //1
var n5 = Number(NaN);            //NaN

JSON.parse(obj);
JSON.stringify(obj);

Window Object
Document Object
Form Object

var uri = “EmpDetails.asp?Emp=årpit&mode=edit”;
document.write(encodeURI(uri) + “
”);
document.write(decodeURI(uri) + “
”);

EmpDetails.asp?Emp=%C3%A5&mode=edit
EmpDetails.asp?Emp=årpit&mode=edit.


The splice() method adds/removes items to/from an array, and returns the removed item(s).
array.splice(index,howmanyItemfordelete,item1,.....,itemX)


var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.splice(2, 0);//Banana,Orange,Apple,Mango

fruits.splice(2, 1);//Banana,Orange,Mango

fruits.splice(2, 2);//Banana,Orange

 fruits.splice(2, 0, "Lemon", "Kiwi"); //Banana,Orange,Lemon,Kiwi,Apple,Mango

fruits.splice(2, 1, "Lemon", "Kiwi");//Banana,Orange,Lemon,Kiwi,Mango

fruits.splice(2, 2, "Lemon", "Kiwi");//Banana,Orange,Lemon,Kiwi

splice always first delete and add the elements

array=[1,2,3];
number=2;
for(var i = array.length - 1; i >= 0; i--) {
    if(array[i] === number) {
       array.splice(i, 1); //1,3
//delete array[i]; //1,"",3
    }
}
alert(array);//1,3


No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...