GP_JS_Objects
GP_JS_Objects
Overview
JavaScript objects are a collection of properties in a key-value pair. These objects can
be understood with real-life objects, like similar objects have the same type of
properties, but they differ from each other.
Example: Let’s say a ball is an object and have properties like ‘colour’ and 'radius'. So
every ball will have the same properties, but different balls will have different
values to them.
1
Example : var obj = {
key1: "value1",
key2: 12345,
"key3": true,
key4: function( ) {
//code
}
}
Creating an Object
1. Using curly brackets -
The properties can be created at the time of creating an object and also after that. Both
creating and accessing the properties share similar syntax.
1. Using a dot operator - You can use dot operator only when the property
name starts with a character. Property can be accessed like - obj.propertyName.
Similarly, you can create property like - obj.propertyName = value
2
2. Using a square bracket - You need to use a square bracket when the key
name starts with a number. If the name contains a special character, then it will
be stored as a string. Property is accessed like - obj["propertyName"].
Similarly, you create property like - obj["propertyName"] = value
NOTE: If you access a property that has not been defined, then undefined is returned.
You can also set function as the value to the key. So the key then becomes the
method name and need parentheses to execute. So you can execute methods like -
obj.methodName( ) and obj["methodName"]( ).
Deleting Property
You can remove property of object using delete operator followed by the property
name. You can either use dot operator or square bracket notation.
3
How are Objects Stored
There are two things that are very important in objects -
These two are important in regard that object variables point to the location where
they are stored. This means that more than one variable can point to the same
location.
Until now, you are creating new objects every time like -
The above two lines will create two different objects are not therefore equal -
But, if you assign one object to another, then the value of 'item1' gets assigned to
'item2', and therefore, they both will point to the same location -
Iterating Objects
JavaScript provides a special form of loop to traverse all the keys of an object. This loop
is called 'for...in' loop.
4
Here the 'variable' gets assigned the property name on each iteration, and 'object' is
the object you want to iterate. Use the square bracket notation with variables to
access the property values.
The iteration may not be in a similar order as to how you see properties in objects
or how you have added them because the objects are ordered specially.
The property names as integers are iterated first in ascending order. Then the other
names are iterated in the order they were added.
ARRAY AS OBJECT
Arrays are actually objects. If you use the typeOf( ) method on an array, you will see
that it will return an object. If you see an array on a console, they are key-value pairs,
with the positive integers as the keys.
Arrays can also store properties just like objects.
Example : array["one"] = 1;
array.one ; // 1
array["one"] ; // 1
5
Arrays vs Object
● Arrays have a length property that objects does not have.
● You can access the values of the arrays like - array[0]; or array["0"]; whereas in
objects, you have to use double quotes ( "" ) only.
● Only when you use an integer as a key, it will change the 'length' property.
● Adding a non-integer key will not have any effect on the length' property.
NOTE: Length property will be set according to the maximum integer key of the array.
❖ But, it also contains the property "four: 40", but it does not show in the
array. But if you use the for-in loop to traverse it, you can traverse all the
properties.
for(var i in arr) {
console.log( i, ":", arr[i]);
}
Output : 0 : 10
1 : 20
2 : 30
four: 40
6
this keyword
Define a function to get the full name of a person in the object-person
var person = {
firstName: "Tony",
lastName : "Stark",
age : 40 ,
getname: function( ) {
return this.firstName + " " + this.lastName;
}
};