JavaScript Learning Object, Constructor and Object Oriented


understand First what is Object-oriented programming (OOP): The basic idea of object-oriented programming is that objects are used to model real world things that we want to represent inside our programs.

What are objects?
Objects can contain related code and data, representing information about the thing we are trying to model, and functionality that we want it to have. Object data is stored neatly inside an object package, making it easy to structure and access. objects created using object literal are singletons. This means when we made a change in an object, it affects the object entire the script. Whereas if an object is created using the constructor function, then the change will not affect the object throughout the script.

// constructor function
function Person(){};

// literal notation
const Person = {};

Objects created using object literal


Since these are singletons, a change to an object affects the object's entire script. A change in one instance will affect all the instances of the object. Object literal is a comma-separated list of name-value pairs inside the curly braces. The object literals encapsulate the data. This minimizes the use of global variables, which can cause problems when combining the code.

const Person = {
        firstName = 'Pradeep',
        lastName = 'Bhosle',
        gender: 'Male',
        }

Objects created using the constructor function

 function person(firstName, lastName, gender){
        this.firstName = firstName;
        this.lastName = lastName;
        this.gende = gender;
    }
// creating object 'new' Keywords let pradeep = new person('Pradeep', 'Bhosle', 'Male') ; let Sanvi = new person('Sanvi', 'Testing', 'Female'); console.log(pradeep, Sanvi);

Comments