Last active
April 15, 2018 12:50
-
-
Save shobhitchittora/eabca079af409f8bc954ad5537a3fe67 to your computer and use it in GitHub Desktop.
JS Design Patterns - CONSTRUCTOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// creating a new empty object | |
const a = new Object(); | |
// defining a constructor | |
function User(id, name){ | |
this.id = id; | |
this.name = name; | |
} | |
// creating a new object using the constructor | |
const myUser = new User(1, 'Bruce'); | |
console.log(myUser); // User {id: 1, name: "Bruce"} | |
// or you can use ES6 classes | |
class Drink{ | |
constructor(price, calories){ | |
this.price = price; | |
this.calories = calories; | |
} | |
} | |
// now create a new user using the new keyword | |
const myDrink= new Drink(15, 200); | |
console.log(myDrink); // Drink {price: 15, calories: 200} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment