Created
November 11, 2014 17:58
-
-
Save octosteve/87b0dda3e8c923ad6f84 to your computer and use it in GitHub Desktop.
A quick illustration of closures in JavaScript
This file contains 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
var InnerSchool = (function(){ | |
function Course(name){ | |
this.name = name; | |
} | |
function School(name){ | |
this.name = name; | |
this.courses = []; | |
} | |
School.prototype.addCourse = function(courseName){ | |
this.courses.push(new Course(courseName)); | |
}; | |
School.prototype.welcome = function(){ | |
console.log('Welcome to ' + this.name ); | |
} | |
return School; | |
})(); | |
var fis = new InnerSchool("The Flatiron School"); | |
fis.addCourse("JavaScript"); | |
console.log(fis.courses); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment