Page 217 - Web_Application_v2.0_C12_Fb
P. 217

In terms of concept, objects are the same across all programming languages. They represent entities from
                 the outside world that we want to express in our programs through properties, and methods. For instance,
                 your object will include properties like name, age, address, id, etc., as well as methods like updateAddress,
                 updateName, etc., if it is a student.
                 Consider an object in JavaScript as a list of items, each of which is a property or method that is kept as a
                 key-value pair in memory.

                 For example:

                    const student = {Name : "Uma", age : 15, Address  : "Green Park" };


                              Object              Key                      Value


                 In JavaScript, the named values are called properties.

                                               Property                             Value

                                   Name                               Uma
                                   Age                                15

                                   Address                            Green Park


                 Creating a JavaScript Object
                 You can define and create your own objects in any of the following ways:

                   Using an object literal.
                    The following example creates a new JavaScript object with three properties:

                    const student = {Name : "Uma", age : 15, Address  : "Green Park" };
                                                                   OR

                    Create an empty JavaScript object, and then add the properties using dot notation as shown:

                    const student = {};
                    student.Name = "Uma";
                    student.age = 15;
                    student.Address = "Green Park";
                   Using the keyword new.
                    The following example creates a new JavaScript object using new Object(), and then adds 3 properties:

                    const student = new Object();
                    student.Name = "Uma";
                    student.age = 15;
                    student.Address = "Green Park";
                   Use an object constructor, and then create objects of the constructed type.
                    When we want to create an object “type” that can be used repeatedly without having to redefine the object
                    each time, constructors can be helpful. This can be done by utilising the Object Constructor function.
                    For example:

                    function Car(Cname, Cmodel) {

                                                                                                  JavaScript Part 2  215
   212   213   214   215   216   217   218   219   220   221   222