Created
August 8, 2023 16:08
-
-
Save suhailgupta03/dba0f5a59f6ec8e056c83ef6f9fdc0f0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// var dog = { | |
// name: 'Rex', | |
// age: 2, | |
// color: "brown", | |
// } | |
function Dog(name, breed, color) { | |
this.name = name; | |
// this keyword refers to the current object | |
// this is the object that is created when the | |
// function is called with new keyword | |
this.breed = breed; | |
this.color = color; | |
} // whenever you use this inside a function | |
// the function is called constructor function | |
// and the name of the function is the class name | |
const rex = new Dog('Rex', 'German Shepherd', 'brown'); | |
// new in JS is used to create a new object | |
console.log(rex); | |
const lassie = new Dog('Lassie', 'Collie', 'white'); | |
const buzo = new Dog('Buzo', 'Labrador', 'black'); | |
this
means object (instance of the class)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// SPORTS is a class
// cricket is an object (or an instance of this class)
// tennis is an object (or an instance of this class)
function Sports(name, totalPlayers) {
this.game = name;
this.totalP = totalPlayers;
}
const cricket = new Sports("Cricket", 11);
cricket.printTotalPlayers();
const tennis = new Sports("Tennis", 2);
tennis.printTotalPlayers();