oi
See we used the string instead of number for accessing elements.Other way to access them is by using the 'dot notation'.
This will work only if the key don't have any special characters, such as spaces.
Just like the accessing the items we have to ways to add new items to an object.
Kitty["color"] = "Brown";
Learning Javascript Part IV
So till now we know what are arrays in javascript. In this pot we are going to talk about Objects.
OOP Concepts are really very vast section to explore,So we are just going to have a introduction of them.
OOP Concepts are really very vast section to explore,So we are just going to have a introduction of them.
Objects in JavaScript
Objects are just like arrays, but they use strings instead of numbers to access the
different elements. These strings used are called keys or properties, and the elements they point to are called values. But unlike the arrays which stores multiple values the objects usually represent single thing's multiple characteristics.Creating Objects in Javascript
There are two ways to create objects in javascript.
var myObject = new Object();
OR
var myobject = {}; // Short way
Lets create new object called Kitty. It will have information about our cat.
Accessing Values
Just like Arrays you can access values in objects using square brackets.See we used the string instead of number for accessing elements.Other way to access them is by using the 'dot notation'.
This will work only if the key don't have any special characters, such as spaces.
Adding Items to the Objects
Just like the accessing the items we have to ways to add new items to an object.
With Strings
Kitty["color"] = "Brown";
With Dot Notation
Array Of objects
You can create arrays of objects i.e. is combine them together.Well this is useful when we want to create a lot of similar objects.Arrays can help us to organize these objects. Lets say we want to make an object storing data of different items in a shop.So we can do this like this,
but it can get quickly get unorganized in a number of situations like what if you want to change price of each items or if we want know how many items are there in total.
An array of objects can be made like this
Now we can find how many items are there in total as every thing is organised.
To access values in objects we can first access the object as an element of array and then access values as we usually do
This is it for objects.Next we are going to take a look at Functions................So a Have great day..s till next time
Learning JavaScript VI
Learning JavaScript V
Learning Javascript Part III
Learning Javascript Part II
Learning JavaScript VI
Learning JavaScript V
Learning Javascript Part III
Learning Javascript Part II
Comments
Post a Comment