Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created November 11, 2014 17:58
Show Gist options
  • Save octosteve/87b0dda3e8c923ad6f84 to your computer and use it in GitHub Desktop.
Save octosteve/87b0dda3e8c923ad6f84 to your computer and use it in GitHub Desktop.
A quick illustration of closures in JavaScript
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