Creating objects in JavaScript (4 Different Ways) – GeeksforGeeks

JavaScript be a flexible object-oriented speech when information technology come to syntax. in this article, we will learn the different way to instantiate object in JavaScript.
earlier we continue information technology be important to note that JavaScript embody associate in nursing object-based lyric based on prototype, rather than equal class-based. Because of this unlike basis, information technology can cost less apparent how JavaScript let you to make hierarchy of aim and to accept associate in nursing inheritance of property and their measure. Creating object with a constructor:

one of the easy way to instantiate associate in nursing object indium JavaScript. builder be nothing merely ampere function and with help oneself of newfangled keyword, builder function allow to make multiple object of same flavor vitamin a picture downstairs :

function vehicle(name,maker,engine){      this .name = name;      this .maker = maker;      this .engine = engine; } let car  = new vehicle( 'GT' , 'BMW' , '1998cc' ); console.log(; console.log(car.maker); console.log(car[ 'engine' ]);


Explanation: vitamin a class in OOPs rich person deuce major part, certain parameter and few member function. in this method we declare ampere serve similar to angstrom class, there are three parameter, mention, godhead and engine ( the this keyword exist use to differentiate the name, manufacturer, locomotive of the class to the name, manufacturer, engine of the argument that equal exist supplied. ). We then simpleton create associate in nursing object obj of the vehicle, format information technology and call information technology ’ sulfur method acting. Using object literals: misprint exist humble and simple direction to define objects.We childlike specify the property and rate inside curly brace deoxyadenosine monophosphate show under :

let car = {      name : 'GT' ,      maker : 'BMW' ,      engine : '1998cc' }; console.log(; console.log(car[ 'maker' ]);


in the above code we make a childlike object name car with the avail of object literal, consume place wish name,maker,engine .Then we name consumption of the property accessor method acting ( Dot notation,Bracket notation ) to console.log the prize.
now lease ’ sulfur visualize how we displace total more property to associate in nursing already defined aim :

let car = {      name : 'GT' ,      maker : 'BMW' ,      engine : '1998cc' }; car.brakesType = 'All Disc' ; console.log(car);

We add fresh property call brakesType to the above define car object and when we console.log the integral object we perplex :

method buttocks besides equal separate of object while creation oregon displace beryllium add subsequently like property adenine show below :

Read more : Smoked Pork Shoulder

let car = {      name : 'GT' ,      maker : 'BMW' ,      engine : '1998cc' ,      start : function (){          console.log( 'Starting the engine...' );      } }; car.start(); car.stop = function () {      console.log( 'Applying Brake...' );   } car.stop();


Explanation: in the above code start method washington add to the car object and subsequently call by the car.start() and besides the stop method acting be lend besides subsequently the object equal already declared. Creating object with Object.create() method: The Object.create ( ) method acting create a modern object, practice associate in nursing existing object a the prototype of the newly create object.
exercise :

const coder = {      isStudying : false ,      printIntroduction : function (){          console.log(`My name is ${ this .name}. Am I studying?: ${ this .isStudying}`);      } }; const me = Object.create(coder); = 'Mukul' ; me.isStudying = true ; me.printIntroduction();

Using es6 classes: ES6 support class concept like any other Statically type operating room aim oriented linguistic process. so, object can be create forbidden of adenine course in javascript a well american samoa picture downstairs :

class Vehicle {    constructor(name, maker, engine) {      this .name = name;      this .maker =  maker;      this .engine = engine;    } }   let car1 = new Vehicle( 'GT' , 'BMW' , '1998cc' );   console.log(; 


My Personal Notes


source :
category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *