Last active
March 28, 2021 14:07
-
-
Save wch/7090027 to your computer and use it in GitHub Desktop.
Namespace example in Javascript. This also demonstrates the module pattern.
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
// mynamespace is an object to use as a namespace | |
mynamespace = (function() { | |
// Variables in the namespace | |
var mynamespace = { | |
foo: "Yes, this is foo." | |
}; | |
// "Public" methods for the namespace | |
mynamespace.fooTwo = function() { | |
return twice(this.foo); | |
}; | |
// "Private" methods in the namespace | |
function twice(x) { | |
return x + x; | |
} | |
// A class in the namespace | |
mynamespace.CoolClass = (function() { | |
// Contstructor | |
var coolclass = function() { | |
this.bar = "A bar."; | |
}; | |
// Convenience var for the prototype | |
var prototype = coolclass.prototype; | |
// "Public" methods - add to the prototype | |
prototype.barThree = function() { | |
return thrice(this.bar); | |
}; | |
// "Private" methods - functions starting with "_" are private only by | |
// convention. | |
prototype._barNine = function() { | |
return thrice(thrice(this.bar)); | |
}; | |
// Internal functions - these are captured in the closure, note that they | |
// are not duplicated when you do `new CoolClass()`. They cannot access | |
// `this`. | |
function thrice(x) { | |
return x + x + x; | |
} | |
return coolclass; | |
})(); | |
// Instantiate the CoolClass (must be after CoolClass is defined) | |
mynamespace.coolObject = new mynamespace.CoolClass(); | |
return mynamespace; | |
})(); | |
// Outside of the anonymous function, can access the following: | |
console.log(mynamespace.foo); // "Yes, this is foo." | |
console.log(mynamespace.fooTwo()); // "Yes, this is foo.Yes, this is foo." | |
// The instantiated CoolClass object, and public members | |
console.log(mynamespace.coolObject); | |
console.log(mynamespace.coolObject.bar); // "A bar." | |
console.log(mynamespace.coolObject.barThree()); // "A bar.A bar.A bar." | |
// Constructor for CoolClass | |
console.log(new mynamespace.CoolClass()); |
Good example tho
Good.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Namespace.ModuleObj = class ModuleObj {} //preguntame wey ...