Hi, this is my 8th js tutorial which describes about
the objects in JavaScript. Objects are very serious topic and we must have
clear idea about objects in js for advance developments. After this object
tutorial we will move on to an existing example.
So let’s move on…👉
If we consider
object as a data type of js we can came to another judgment, it has five
datatypes. Which means three primitive data types (numbers, string, Boolean),
null and undefined data types.
An object is a list of primitive data types. We can initialize values to object as arrays or we can leave them empty. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value. It is the simplest way to explain object.
OK let’s move to create an object.
This is a simple object …
An object is a list of primitive data types. We can initialize values to object as arrays or we can leave them empty. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value. It is the simplest way to explain object.
OK let’s move to create an object.
This is a simple object …
Think as object is a
list that contain item. So, according to the above example it is stored as name
value pair. The property names are first_Name and second_Name and the values
are chamini and prashakthi.
We can use strings or numbers as property names.
Like,
var marks={50:”bad”,75:”good”,100:”very good”};
Also we can create a js object using new operator.
var object1= new Object();
var object2= new Array(“dog”, ”cat”, ”cow”, ”tiger” );
If we create an empty object we can assign a value to that empty
object in this way.
objetName.objectProperty = propertValue ;
Object methods
Objects do something using object methods. You know that all the previous tutorials we
use document.write syntax. So here the document is an object and write() is a method in
the js.
Object constructor
This is really important point to understand. So I will explain it
in the simplest way. If you have ever learn any of object oriented language you
may heard about constructors.
If not see below explanation.
Constructor is a function.
The usage of a constructor is, it is use to create and initialize an object. Js
provide object()function to create an object. Also we can assign or initialize a variable
to an object using return value of the object() constructor.
Check the first demonstration.
Figure 1
Also we can use first method that I described in the start
of the tutorial.
Figure 2
This is the result of both examples:
Figure 3
We can use user defined functions to initialize objects. Same
example with a user define function:
OK this is the end of js objects part one. I will bring up more examples and new theories with next tutorial js object part two.
thank-you.
✋
Comments
Post a Comment