Skip to content

Instantly share code, notes, and snippets.

@shobhitchittora
Last active April 15, 2018 12:50
Show Gist options
  • Save shobhitchittora/eabca079af409f8bc954ad5537a3fe67 to your computer and use it in GitHub Desktop.
Save shobhitchittora/eabca079af409f8bc954ad5537a3fe67 to your computer and use it in GitHub Desktop.
JS Design Patterns - CONSTRUCTOR
// 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