JavaScript Practice Day1 - Constructor notation and Literal Notation

Constructor notation - This is one of the method of creating object in javascript  . All the objects  created from Constructor Notation are differerent and if an object is changed and it does not change the property of other objects .In the below example we are creating a class Employee with two properties firstName and LastName and a method getFullName to return the full name.




 //object creation with  constructor function
    function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

        this.getFullName = function () {
            return this.firstName + " " + this.lastName;
        }

    }
    //object creation with constructor function


Creating the object from the class template. We have created an employee2 object and passing the parameters "Amit" and "Mazumder" to the class constructor
    var employee2 = new Employee("Amit", "Mazumder");

//constructor notation is not singleton but literal notation is singleton    . We have two objects employee2 and newEmployee2 both have firstName="Amit" and lastName="Mazumder".After the name is changed for one objects for newEmplouee2 employee2 is unaffected.

var newEmployee2 = new Employee("Amit","Mazumder");


       // before the name is changed
        console.log("before the name is changed");
        console.log("Employee object"+employee2.getFullName());
        console.log("New Employee "+newEmployee2.getFullName());
        

      //After the name is changed
        console.log("After the name is changes");
        newEmployee2.firstName="Kalyan";
        console.log("Employee object"+employee2.getFullName());
        console.log("New Employee "+newEmployee2.getFullName());
        


Literal Notation : This is another notation of creating javascript objects ,we are not using = symbol but use  : to assign properties the required values  Literal Notation of creation  of object is singleton that mean if all the objects created from the same class and if  for a particular object the property is changed the change reflects in all the class

// object creation with literal notation

    var employee =
        {
            firstName: "Amit",
            lastName: "Mazumder",

            getFullName: function () {
                return this.firstName + " " +this.lastName;
            }
        }

    var newEmployee = employee;


With Literal Notation we have two objects employee and newEmployee , and if we change the property of newEmployee it also changes the property of employee

// before the name is changed
        console.log("before the name is changed");
        console.log("Employee object"+employee.getFullName());
        console.log("New Employee "+newEmployee.getFullName());
        
        
        //After the name is changed
        console.log("After the name is changes");
        newEmployee.firstName="Kalyan";
        console.log("Employee object"+employee.getFullName());
        console.log("New Employee "+newEmployee.getFullName());
        

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6