Page 218 - Web_Application_v2.0_C12_Fb
P. 218

this.name = Cname;
                          this.yr_model = Cmodel; }
                  car1 = new Car('BMW', '2019');
                  car2 = new Car('Santro', '2020');
                       Do you know?


                      this in JavaScript refers to the object that is executing the current function, and its value depends
                      on how the function is called.


                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
              Properties and Methods

              In JavaScript, objects are collections of properties and methods.
                Properties: describe the characteristics of the object (key-value pairs).

                Methods: functions defined inside an object that perform actions using the object's data.
              A property is a variable attached to an object, defined as a key-value pair.

              The syntax to define an object with properties in JavaScript is as follows:

                  const objectName = {
                      key1: value1,
                      key2: value2,
                      key3: value3

                  };
              Example 27: To demonstrate the use of object properties and methods

                  <!DOCTYPE HTML>
                  <HTML>

                216   Touchpad Web Applications (Ver. 2.0)-XII
   213   214   215   216   217   218   219   220   221   222   223