Created
November 27, 2020 01:52
-
-
Save misterpoloy/ae190428b314fff2b8860af925b94bad to your computer and use it in GitHub Desktop.
Constructor Pattern
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
function Human(name, age, occupation){ //ES5 function based constructor | |
//defining properties inside the constructor function | |
//constructor initializing the property values upon object creation | |
this.name = name; | |
this.age = age; | |
this.occupation = occupation; | |
//defining a method inside the constructor function | |
this.describe = function(){ | |
console.log(`${this.name} is a ${this.age}-year-old ${this.occupation}`); | |
} | |
} | |
//creating a "person" object using the Human constructor | |
//the constructor uses the arguments passed into it to | |
//initialize the property values name, age and sex | |
var person = new Human("Elle", "23", "Engineer"); | |
//calling the describe method for the person object | |
person.describe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With ES6: