contructor can be thought of as a template.
For retrieving property values , we have two ways, dot notation and bracket notation
bracket notation has an advantage where we can use variable which store property names as well.
ex :
var somobj = { propname : "propval"};
var p = "propname";
somobj[p]; // this also means somobj["propname"] , hence this is the advantage.
** sometimes we need to know the type of variable we are using, it can be 'number', 'string','function' or 'object'.
so to find this , we have a function to know this, 'typeof()'
ex :
var a = { b : "dfd"};
console.log(typeof(a)); // o/p is object
****** All objects have a useful function, which tells if an object has a certain property or not, that is 'hasOwnProperty'
ex:
return somobj.hasOwnProperty('name') ; // returns true or false, if it has that property then true.
** for in loop can be used to print all the property names.
we can also use this to print the property values if we use bracket notation.
ex :
var somobj = { type : "indie" }
for ( var prop in somobj){
console.log(prop); // print the name of property, i.e, o/p is type
console.log(somobj[prop]); // print the value of property
For retrieving property values , we have two ways, dot notation and bracket notation
bracket notation has an advantage where we can use variable which store property names as well.
ex :
var somobj = { propname : "propval"};
var p = "propname";
somobj[p]; // this also means somobj["propname"] , hence this is the advantage.
** sometimes we need to know the type of variable we are using, it can be 'number', 'string','function' or 'object'.
so to find this , we have a function to know this, 'typeof()'
ex :
var a = { b : "dfd"};
console.log(typeof(a)); // o/p is object
****** All objects have a useful function, which tells if an object has a certain property or not, that is 'hasOwnProperty'
ex:
return somobj.hasOwnProperty('name') ; // returns true or false, if it has that property then true.
** for in loop can be used to print all the property names.
we can also use this to print the property values if we use bracket notation.
ex :
var somobj = { type : "indie" }
for ( var prop in somobj){
console.log(prop); // print the name of property, i.e, o/p is type
console.log(somobj[prop]); // print the value of property
No comments:
Post a Comment