Last active
March 30, 2018 19:47
-
-
Save nodlAndHodl/d3fae7a31340f8b040ef5c71f4b4a246 to your computer and use it in GitHub Desktop.
Javascript Create New Object
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
//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