/**************** OOPS IN JAVASCRIPT *************************/ /* JAVASCRIPT AND CLASSES So yes, JavaScript does have classes, and they are quite powerful for object-oriented programming! /* OOP /*Object -collection to properties and methods -ex: toLowerCase /* parts of oops Object literal means literally assinged -constructor functions -prototypes -classes n -Instances /* 4 pillars abstraction encaplusation inheritnace polymoriphism */ const user = { // object literal username: "hitesh ", logincount: 8, signedIn: true, getUserDetails: function() { console.log(`username: ${this.username}`); console.log(this); /* output : { username: 'hitesh ', logincount: 8, signedIn: true, getUserDetails: [Function: getUserDetails]*/ } } console.log(user.getUserDetails()); // username: hitesh //undefined console.log(this); // {} /////////////// CONSTRUCTOR FUNCTION ////////////// function User2(username, logincount, isLoggedIn) { this.username = username; this.logincount = logincount; this.isLoggedIn = isLoggedIn; this.greeting = functio return this } const userOne =/*HERE new TO FIX THE ERROR*/ User2("hitesh", 12, true); const userTwo = /*HERE new TO FIX THE ERROR*/User2("hitesh chaudhary", 18, false) console.log(userOne); /*username: 'hitesh chaudhary', logincount: 18, isLoggedIn: false*/ /* INPORTANT == IF WE USE NEW IN DECLARATION SPACE IT WILL RUNCORRECTLY */ /* INPORTANCE and FEATURES OF NEW KEYWORD === when we use new keyword new empty object is made in first step , it will call the constructor function in second step , it will inject all the values which we passed in arguments , than it will return to you */
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter