JS Object

/*
*object in js is an entity holding key and value pairs
*key will be a valid js variable name(no special charater)
*below rules applies, see numObj variable
*/
var numObj = {
1: "a",//here key is 1 (a number) which is acceptable but not recommended
45: "t",
//1m : "we",//js says if key start with no, it has to be number only
m1: "he",//while alplhabet followed by number is fine
_12m: "so"//_ can be followed by alphnumeric character
};
//while it is recommended in JS to access any object key by using dot to access value corresponds to a key
//like numObj.m1, numObj._12, although can also be referenced by numObj["m1"],objNum["_12"] but not recormmended

//console.log(numObj.45);//exception : js complain about it not a valid expression to access value
//numeric key cannot accessed using dot
console.log(numObj["1"]);//will print "a"
console.log(numObj["45"]);//will print "t"
console.log(numObj[45]);//will print "t", note for key declared with No only can be accessed like this,alphanumeric key cannot
console.log(numObj[49]);//will print undefined.

//whenever a number is given as index or key,JS treats them in a special way
//if the number given really exits as a key or index Js will internally convert to number or string to try to get value.
//if found will print the vlue else say undefined

//however the case with an alphanumeric key or index is different.
//if it is given as quoted index "_12m" if this key exits ,value will be printed else undefined
//however if given with dot notation, JS thinks you are defining it but you didn't assign value and printing it gives undefined
//but the alphnumeric key given in index without quotes is not at all allowed(except point A), JS will throw error :
console.log(numObj["_12m"]);//will print "so"
console.log(numObj["mmm"]);//will print undefined
console.log(numObj.mmm);//will print undefined
//console.log(numObj[_12m]);//ReferenceError: _12m is not defined

//point A : however if key is a defined variable like :
var t132;
console.log(numObj[t132]);//will print undefined

//while key starts with alphabetnumeric can be accessed by dot or ["key"] notation
console.log(numObj["m1"]);//will print "he"
console.log(numObj._12m);//will print "so"
//console.log(numObj."45");//not correct, only alphanumeric identifier is allowed.

//an object can be a collection of heterogeneous items(like array), :
var book = { // Objects are enclosed in curly braces.
topic: "JavaScript", // The property "topic" has value string "JavaScript".
fat: true, // The property "fat" has value boolean true.
//below property "loc" has value an array which again holding heterogenous items (like "ert",number,boolean and even function pointer)
loc: ["ert", 12, true, function () { console.log("under func"); }],
fun: function (prm) {//property "kru" holds referenc to a function
console.log("obj func=" + prm);
},
"lit": "fine",//string key , although this property can be accessed the same way like other keys
true: true//property name is a boolean keyword(not recommeneded) and value is also boolean
};

console.log(book);
//this will print
/**
{ topic: 'JavaScript',
fat: true,
loc: [ 'ert', 12, true, [Function] ],
fun: [Function: fun],
lit: 'fine',
true: true }
*/

//to access property
console.log(book.loc);//will print [ 'ert', 12, true, [Function] ]

//like array , an object can also be updated with a new proeprty
book.newprop = "this is new property";//added a new property to main "book" object
book.loc["yy"] = { lof: "poo" };//added a new item "yy" to array represented by "loc" property of "book" object
book.loc.abc = { lof: "poo" };//added a new item "abc" to array represented by "loc" property of "book" object
book.loc["so"] = function () {//added a new function pointer item to array represented by "loc" property of "book" object
console.log("Mill");
};
console.log(book);//will print:
/**
{ topic: 'JavaScript',
fat: true,
loc:
[ 'ert',
12,
true,
[Function],
yy: { lof: 'poo' },
abc: { lof: 'poo' },
so: [Function] ],
fun: [Function: fun],
lit: 'fine',
true: true,
newprop: 'this is new property' }
*/

Comments

Popular posts from this blog

Custom Module Export

JS prototype

JS Array