Created
May 3, 2014 09:39
-
-
Save greggman/e9f93c2eedcb8c489fbe to your computer and use it in GitHub Desktop.
Closure no like?
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
/** | |
* Some class | |
* @constructor | |
* @param {string} name The name. | |
*/ | |
var Employee = (function(){ | |
// private static field | |
// @type {string} | |
var staticVar; | |
// class function a.k.a. constructor | |
// @constructor | |
// @param {string} name2 | |
var cls = function(name2) { | |
// private instance field | |
// @type {string} | |
var name = name2; | |
// public instance field | |
// @type {number} | |
this.age = 10; | |
// private instance method | |
var increment = function() { | |
++this.age; | |
}.bind(this); | |
// public instance method | |
// @return {string} | |
this.getName = function() { | |
return cls.capitalize(name); | |
}; | |
// @param {string} name2 the name | |
this.setName = function(name2) { | |
name = name2; | |
}; | |
// | |
this.increment = function() { | |
increment(); | |
}; | |
// | |
// @returns number; | |
this.getAge = function() { | |
return this.age; | |
}; | |
} | |
// public static field | |
// @type {number} | |
cls.staticVar = 0; | |
// public static method | |
// @param {string} name Name to Capitalize. | |
// @return {string} capitalized name | |
cls.capitalize = function(name){ | |
return name.substring(0, 1).toUpperCase() + | |
name.substring(1).toLowerCase(); | |
}; | |
// private static method | |
// @param {string} name | |
var createWithName = function(name) { | |
var obj = new cls(); | |
obj.setName(cls.capitalize(name)); | |
return obj; | |
} | |
return cls; | |
})(); | |
var e = new Employee("joe"); | |
console.log(e.getName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment