Skip to content

Instantly share code, notes, and snippets.

@nodlAndHodl
Last active March 30, 2018 19:47
Show Gist options
  • Save nodlAndHodl/d3fae7a31340f8b040ef5c71f4b4a246 to your computer and use it in GitHub Desktop.
Save nodlAndHodl/d3fae7a31340f8b040ef5c71f4b4a246 to your computer and use it in GitHub Desktop.
Javascript Create New Object
//using the new keyword for creating javascript object. Using a constructor function.
//Links to an object prototype
//Demo on creation of new constructor
let Task = function(name){
this.name = name;
this.completed = false;
}
//using binding of functions to this.prototype
//using this, reduces the creation of new save,
//or complete function everytime a new Task is created.
Task.prototype.save = function(){
//implicitly return this.
console.log("saving task: " + this.name);
}
Task.prototype.complete = function(){
console.log('completing this task: ' + this.name)
//implicitly return this.
this.completed = true;
}
//binds this to new object scope
let task = new Task('demo constructor');
console.log(task.completed))
task.complete()
console.log(task.completed))
task.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment