Created
November 22, 2012 14:49
-
-
Save pyrobot/4131539 to your computer and use it in GitHub Desktop.
JavaScript OOP Example
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
// knowing three things is essential for JavaScript OOP | |
// --- | |
// #1 the concept of "closures" (and functional scoping in general) | |
// #2 knowing how the keyword "this" works, and how to use it. | |
// #3 knowing when to use the keyword "new" | |
// example of an OOP style JavaScript class implementation | |
var ProtoClass = (function(){ | |
// these would be your "private" variables, (in the sense that it can no longer be modified from the outside) | |
// if you send any of this stuff to the client, its clearly visible with any decent browser Developer Tools | |
var privateData = "sekr3tC0des"; | |
// this is your objects "constructor" | |
function ProtoClass() { | |
this.someData = "i'm a true, blue, object!"; | |
this.someValue = true; | |
this.someType = "blue"; | |
} | |
// one big part of OOP was encapsulation right? | |
function privateMethod(stuff) { | |
// I'm so private! Nobody knows what I do, except my scope buddy ProtoClass | |
// I know that i'll be called with the correct "this" context | |
// So I can even access all of ProtoClass's public variables! | |
console.log("i'm a " + this.someValue + ", " + this.someType + ", private method!"); | |
console.log("i was called with: " + stuff + ", but i can access the private stuff too: " + privateData); | |
} | |
// this method will appear as part of the object, ie "public" | |
ProtoClass.prototype.innerMethod = function (stuff) { | |
// "call" the function "privateMethod" and be sure to pass the ProtoClass instance as the functions "this" context | |
privateMethod.call(this, stuff); | |
} | |
// you can even do class factory type stuff! | |
ProtoClass.newObject = function () { | |
return new ProtoClass(); | |
} | |
return ProtoClass; | |
})(); | |
// grab an instance of a new class out of the class factory, and immediately start using it! | |
// (making for some ugly code in the process) | |
console.log(ProtoClass.newObject().someData = "You didn't save me to a var... great, so now i'm going to be garbage."); | |
// make sure to call with the "new" keyword | |
console.log("Creating a *new* ProtoClass in 3.. 2.. 1..") | |
var obj = new ProtoClass(); | |
// this will work | |
console.log("Time to call some stuff that exists:") | |
obj.innerMethod("passedInStuff"); | |
// this also will work | |
console.log("obj.someData = " + obj.someData); | |
console.log("obj.someValue = " + obj.someValue); | |
console.log("obj.someType = " + obj.someType); | |
// none of this will work, both are undefined | |
console.log("Time to call some stuff that doesn't exist:") | |
try { | |
obj.privateMethod(); | |
} | |
catch (err) { | |
console.log("Caught: " + err) | |
} | |
// obj == null is a shortcut to check if something was defined | |
// in most other cases, you should normally do deep comparison ie: (a === b) | |
if (obj.privateData == null) { | |
console.log("obj.privateData is most definitely " + obj.privateData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
outputs:
You didn't save me to a var... great, so now i'm going to be garbage.
Creating a new ProtoClass in 3.. 2.. 1..
Time to call some stuff that exists:
i'm a true, blue, private method!
i was called with: passedInStuff, but i can access the private stuff too: sekr3tC0des
obj.someData = i'm a true, blue, object!
obj.someValue = true
obj.someType = blue
Time to call some stuff that doesn't exist:
Caught: TypeError: Object # has no method 'privateMethod'
obj.privateData is most definitely undefined