Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created November 27, 2020 01:52
Show Gist options
  • Save misterpoloy/ae190428b314fff2b8860af925b94bad to your computer and use it in GitHub Desktop.
Save misterpoloy/ae190428b314fff2b8860af925b94bad to your computer and use it in GitHub Desktop.
Constructor Pattern
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();
@misterpoloy
Copy link
Author

With ES6:

class Shape{
    constructor(color, sides, name){
        this.color = color; 
        this.sides = sides;
        this.name = name;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment