Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 8, 2023 16:08
Show Gist options
  • Save suhailgupta03/dba0f5a59f6ec8e056c83ef6f9fdc0f0 to your computer and use it in GitHub Desktop.
Save suhailgupta03/dba0f5a59f6ec8e056c83ef6f9fdc0f0 to your computer and use it in GitHub Desktop.
// 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');
@suhailgupta03
Copy link
Author

// 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;

this.printTotalPlayers = function() {
    console.log(this.totalP);
}

}

const cricket = new Sports("Cricket", 11);
cricket.printTotalPlayers();

const tennis = new Sports("Tennis", 2);
tennis.printTotalPlayers();

@suhailgupta03
Copy link
Author

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