JavaScript tutorial -3 CRUD operations with Object literals

In previous tutorial we have seen how to create object using Object literals, continuing with that let’s see how to manipulate (pay around) the object using object literals in java script .

Getting value from object is easier.

print(myObj2.name); // Will work 
print(myObj2['name']); // will work 
print(myObj2.parent.father_name) // will work 
print(myObj2.parent['father_name']) // will work 
print(myObj2.['parent.father_name']) // will not work 

What happens when you try to access a property that doest not exist in object

print(myObj2.phoneNumber); // returns undefined

what happens when you try to access a undefined property

print(myObj2.phoneNumber.office); // throw "TypeError" 

Updating an object by property and values.

myObj1.name = 'jhon';
myObj2.age = '22';

The value ‘jhon’ is updated in case the property called “name” already exist in myObj1 object. Otherwise “name” property is created and then the value is assigned to it.

Object are passed as reference. They are copied.


var myObj3 = {
name : "ray"
}
var myObj4 = myObj3;
print(myObj4.name) // returns ray
myObj4.name = 'Twlight'
print(myObj4.name) // returns Twlight
print(myObj3.name) // returns Twlight

Delete
delete operator can be used to delete an property from the object.

delete myObj1.name // deletes the property name from object.

This isn’t the end of object concepts there are still more to learn about object but this is enough for beginner so lets stop here about objects. Jump to function.

To continue the progressive learning: JavaScript tutorial -4 Function

Post a Comment

Your email is never published nor shared. Required fields are marked *