Last active
August 29, 2015 14:11
-
-
Save romyilano/2c0e94c313e311ddf72a to your computer and use it in GitHub Desktop.
javascript for dummies... nifty OOP similar behavior for javascript seen and heard on stackoverflow. it's like... it's not a class but you are like faking it til you making it. gNARLY http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects
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
// is this standard???? | |
var MySnowboard = (function () { | |
// private static | |
var serialNumber = 1; | |
// constructor without any vowels. why? just because. it's hip | |
var snwbrd = function () { | |
// private | |
var id = serialNumber++; | |
var name = 'Deep Moist Unknown'; | |
// public (this instance only) | |
this.get_id = function () { return id; }; | |
// do they also call this getter and setter? | |
this.get_name = function () { return name; }; | |
this.set_name = function (value) { | |
if (typeof value != 'string') | |
throw 'Name must be a string'; | |
if (value.length < 2 || value.length > 20) | |
throw 'Name must be 2-20 characters long.'; | |
name = value; | |
}; | |
}; | |
// public static | |
snwbrd.get_nextId = function () { | |
return nextId; | |
}; | |
// public (shared across instances) | |
snwbrd.prototype = { | |
announce: function () { | |
alert('Hi there! My snowboards id is ' + this.get_id() + ' and its name is "' + this.get_name() + '"!\r\n' + | |
'my next snowboards\'s id will be ' + MySnowboard.get_nextId() + '!'); | |
} | |
}; | |
return snwbrd; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment