A JavaScript object has properties associated with it. A property of an object is explained in a variable which is been assigned to an object. The properties of an object can be accessed with a simple dot-notation.
Syntax :
objectName.propertyName
objectName and propertyName both are case-sensitive. property of an object can be defines the characteristic of an object. For example, let's create an object named person and give its properties named name,place,age and gender as follows :
var person = new Object();
person.name = "ram";
person.place = "Hyderabad";
person.age = 25;
person.gender = "Male";
If the properties of an object are undefined (not null)
person.dob // undefined
The properties of an object can also be set or accessed using bracket notation. Objects are sometimes called as associative arrays. As each property can be accessed with a string value can be used to access it. For example,
person['name'] = "ram";
person['place'] = "Hyderabad";
person.['age'] = 25;
person.['gender'] = "Male";
If a property name starts with a number or hyphen or contains space will not be a valid JavaScript identifier. The square bracket notation is very useful when property names are been determined dynamically.
creating 4 variables and assigning it in a single way, separated with comma(,).
var myobj = new Object();
mystr = "myString";
Syntax :
objectName.propertyName
objectName and propertyName both are case-sensitive. property of an object can be defines the characteristic of an object. For example, let's create an object named person and give its properties named name,place,age and gender as follows :
var person = new Object();
person.name = "ram";
person.place = "Hyderabad";
person.age = 25;
person.gender = "Male";
If the properties of an object are undefined (not null)
person.dob // undefined
The properties of an object can also be set or accessed using bracket notation. Objects are sometimes called as associative arrays. As each property can be accessed with a string value can be used to access it. For example,
person['name'] = "ram";
person['place'] = "Hyderabad";
person.['age'] = 25;
person.['gender'] = "Male";
If a property name starts with a number or hyphen or contains space will not be a valid JavaScript identifier. The square bracket notation is very useful when property names are been determined dynamically.
creating 4 variables and assigning it in a single way, separated with comma(,).
var myobj = new Object();
mystr = "myString";
randomNo = Math.random();
newObj = new Object();
myobj.type = "dotSyntax";
myobj['newstring'] = "This is a string";
myobj[mystr] = "This key is defined in mystr variable";
myobj[randomNo] = "Random Number";
myobj[newObj] = 'This is an Object';
myobj[' '] = 'empty string';
console.log(JSON.stringify(myobj));
output :
{
"type":"dotSyntax",
"newstring":"This is a string",
"myString":"This key is defined in mystr variable",
"0.6610736800691552":"Random Number",
"[object Object]":"This is an Object",
" ":"empty string"
}
All the keys in square bracket notation are converted to a string type. objects in JavaScript can only have string type as key type.JavaScript will call the obj.toString().
Bracket notation can be used in for..in to iterate overall enumerable properties of an object.
For example :
function showProperties(obj,objName){
var result = ' ';
for(var i in obj){
if(obj.hasOwnProperty(i)){
result += objName + '.' + i + '=' + obj[i] + '\n';
}
}
return result;
}
showProperties(person,"person");
output :
This result returns :
"person.name=ram
person.place=Hyderabad
person.age=25
person.gender=Male "
Enumerate the properties of an object :
Starting with the ECMAScript 5, there are three native ways to list the object properties.
- for...in loops
- Object.keys(o);
- Object.getOwnPropertyNames(o);
for...in loops :
for..in statement iterates over the enumerable properties of an object.
Syntax :
for( variable in object) {
.............
}
variable is a different property name i.e, assigned to variable on each iteration.
object is whose enumerable properties are iterated.
A for....in loops iterates over all enumerable properties of the object itself.
Deleted, Added or modified properties :
A for....in loop iterates over the properties of an object in an arbitrary order. If a property is modified in one iteration then later if we visit, we get the update value. If the property is deleted before, then its value is not visited any more. Properties added in the object may be iterated or can be omitted. It would be better not to add,modify or delete the properties during iteration.
Array iteration :
Array indexes are an enumerable properties with integer names and the for...in may not return in any particular order.
The example for for...in is discussed above.
Object.keys() :
This returns the array of given objects in the same order as provided in the for..in loop.
Syntax :
Object.keys(obj)
Examples :
var obj = {'a' : 0, 'b' : 1, 'c' : 2};
console.log(Object.keys(obj));
output :
"a","b","c"
//with random key
var obj1 = {100 : 'a',1 : 'b' , 2 : 'c'};
console.log(Object.keys(obj1));
output :
1, 2 , 100
Object.getOwnPropertyNames() :
This method returns an array of all properties which are been found in the given object. It returns an array whose elements are strings which can be enumerable or non-enumerable properties, found in the obj. The ordering of these properties are not defined.
Syntax :
Object.getOwnPropertyNames(obj);
Examples :
var arr = ['a', 'b'];
console.log(Object.getOwnPropertyNames(arr).sort());
output :
[ "0", "1" , "length"]
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.getOwnPropertyNames(obj).sort());
output :
["0", "1", "2"];
Object.getOwnPropertyNames(obj).forEach(
function (val, idx, array) {
console.log(val + ' -> ' + obj[val]);
}
);
output :
0 -> a
1 -> b
2 -> c
No comments:
Post a Comment