Page 206 - Web Applications (803) Class 12
P. 206

  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) {
                          this.name = Cname;
                          this.yr_model = Cmodel; }


                  car1 = new Car(‘BMW’, ‘2019’);
                  car2 = new Car(‘Santro’, ‘2020’);

                       Do you know?

                      ‘this’ refers to the calling object. In the above example, car1 and car2 are the calling objects.



                Using Object.create().
              The Object.create method can be used to create a new object in JavaScript as shown:

                  var employee = Object.create(null);
                  // Set property to object
                          employee.name = "Simran";
              Attributes/Properties can be combined as follows:

                  const Employee = {
                      EName: “Vinod”,
                      Gender: ‘M’,
                      salary: {
                          basic: 40000,
                          da: 2500,
                          hra: 650

                      }
                  }
              The above object’s properties can be accessed by using the dot notation, for e.g.

                  Employee.Ename          //output Vinod

                  Employee.salary.basic               //output 40000






                204   Touchpad Web Applications-XII
   201   202   203   204   205   206   207   208   209   210   211