Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}
- var yourNamespace = {
- foo: function() {
- },
- bar: function() {
- }
- };
- ...
- yourNamespace.foo();
- (function( skillet, $, undefined ) {
- //Private Property
- var isHot = true;
- //Public Property
- skillet.ingredient = "Bacon Strips";
- //Public Method
- skillet.fry = function() {
- var oliveOil;
- addItem( "tn Butter nt" );
- addItem( oliveOil );
- console.log( "Frying " + skillet.ingredient );
- };
- //Private Method
- function addItem( item ) {
- if ( item !== undefined ) {
- console.log( "Adding " + $.trim(item) );
- }
- }
- }( window.skillet = window.skillet || {}, jQuery ));
- //Adding new Functionality to the skillet
- (function( skillet, $, undefined ) {
- //Private Property
- var amountOfGrease = "1 Cup";
- //Public Method
- skillet.toString = function() {
- console.log( skillet.quantity + " " +
- skillet.ingredient + " & " +
- amountOfGrease + " of Grease" );
- console.log( isHot ? "Hot" : "Cold" );
- };
- }( window.skillet = window.skillet || {}, jQuery ));
- var ns = new function() {
- var internalFunction = function() {
- };
- this.publicFunction = function() {
- };
- };
- var your_namespace = your_namespace || {};
- var your_namespace = your_namespace || {};
- your_namespace.Foo = {toAlert:'test'};
- your_namespace.Bar = function(arg)
- {
- alert(arg);
- };
- with(your_namespace)
- {
- Bar(Foo.toAlert);
- }
- var MYNS = MYNS || {};
- MYNS.subns = (function() {
- function privateMethod() {
- // Do private stuff, or build internal.
- return "Message";
- }
- return {
- someProperty: 'prop value',
- publicMethod: function() {
- return privateMethod() + " stuff";
- }
- };
- })();
- var MYNS = MYNS || {};
- MYNS.subns = (function() {
- var internalState = "Message";
- var privateMethod = function() {
- // Do private stuff, or build internal.
- return internalState;
- };
- var publicMethod = function() {
- return privateMethod() + " stuff";
- };
- return {
- someProperty: 'prop value',
- publicMethod: publicMethod
- };
- })();
- namespace = window.namespace || {};
- namespace.namespace1 = namespace.namespace1 || {};
- namespace.namespace1.doSomeThing = function(){}
- namespace = window.namespace || {};
- namespace.namespace2 = namespace.namespace2 || {};
- namespace.namespace2.doSomeThing = function(){}
- /**
- * My JavaScript application
- *
- * @module myapp
- */
- /** @namespace Namespace for MYAPP classes and functions. */
- var MYAPP = MYAPP || {};
- /**
- * A maths utility
- * @namespace MYAPP
- * @class math_stuff
- */
- MYAPP.math_stuff = {
- /**
- * Sums two numbers
- *
- * @method sum
- * @param {Number} a First number
- * @param {Number} b Second number
- * @return {Number} Sum of the inputs
- */
- sum: function (a, b) {
- return a + b;
- },
- /**
- * Multiplies two numbers
- *
- * @method multi
- * @param {Number} a First number
- * @param {Number} b Second number
- * @return {Number} The inputs multiplied
- */
- multi: function (a, b) {
- return a * b;
- }
- };
- /**
- * Constructs Person objects
- * @class Person
- * @constructor
- * @namespace MYAPP
- * @param {String} First name
- * @param {String} Last name
- */
- MYAPP.Person = function (first, last) {
- /**
- * First name of the Person
- * @property first_name
- * @type String
- */
- this.first_name = first;
- /**
- * Last name of the Person
- * @property last_name
- * @type String
- */
- this.last_name = last;
- };
- /**
- * Return Person's full name
- *
- * @method getName
- * @return {String} First name + last name
- */
- MYAPP.Person.prototype.getName = function () {
- return this.first_name + ' ' + this.last_name;
- };
- var myNamespace = {}
- myNamespace._construct = function()
- {
- var staticVariable = "This is available to all functions created here"
- function MyClass()
- {
- // Depending on the class, we may build all the classes here
- this.publicMethod = function()
- {
- //Do stuff
- }
- }
- // Alternatively, we may use a prototype.
- MyClass.prototype.altPublicMethod = function()
- {
- //Do stuff
- }
- function privateStuff()
- {
- }
- function publicStuff()
- {
- // Code that may call other public and private functions
- }
- // List of things to place publically
- this.publicStuff = publicStuff
- this.MyClass = MyClass
- }
- myNamespace._construct()
- // The following may or may not be in another file
- myNamespace.subName = {}
- myNamespace.subName._construct = function()
- {
- // Build namespace
- }
- myNamespace.subName._construct()
- var myClass = new myNamespace.MyClass();
- var myOtherClass = new myNamepace.subName.SomeOtherClass();
- myNamespace.subName.publicOtherStuff(someParameter);
- Namespace('my.awesome.package');
- my.awesome.package.WildClass = {};
- Namespace('my.awesome.package', {
- SuperDuperClass: {
- saveTheDay: function() {
- alert('You are welcome.');
- }
- }
- });
- var namespace = {};
- namespace.module1 = (function(){
- var self = {};
- self.initialized = false;
- self.init = function(){
- setTimeout(self.onTimeout, 1000)
- };
- self.onTimeout = function(){
- alert('onTimeout')
- self.initialized = true;
- };
- self.init(); /* If it needs to auto-initialize, */
- /* You can also call 'namespace.module1.init();' from outside the module. */
- return self;
- })()
- function namespace(namespace) {
- var object = this, tokens = namespace.split("."), token;
- while (tokens.length > 0) {
- token = tokens.shift();
- if (typeof object[token] === "undefined") {
- object[token] = {};
- }
- object = object[token];
- }
- return object;
- }
- // Usage example
- namespace("foo.bar").baz = "I'm a value!";
- (function(){
- namespace("images", previous, next);
- // ^^ This creates or finds a root object, images, and binds the two functions to it.
- // It works even though those functions are not yet defined.
- function previous(){ ... }
- function next(){ ... }
- function find(){ ... } // A private function
- })();
- var MYNamespace = MYNamespace|| {};
- MYNamespace.MyFirstClass = function (val) {
- this.value = val;
- this.getValue = function(){
- return this.value;
- };
- }
- var myFirstInstance = new MYNamespace.MyFirstClass(46);
- alert(myFirstInstance.getValue());
- global_namespace.Define('startpad.base', function(ns) {
- var Other = ns.Import('startpad.other');
- ....
- });
- var myNamespace = (function () {
- var myPrivateVar, myPrivateMethod;
- // A private counter variable
- myPrivateVar = 0;
- // A private function which logs any arguments
- myPrivateMethod = function( foo ) {
- console.log( foo );
- };
- return {
- // A public variable
- myPublicVar: "foo",
- // A public function utilizing privates
- myPublicFunction: function( bar ) {
- // Increment our private counter
- myPrivateVar++;
- // Call our private method using bar
- myPrivateMethod( bar );
- }
- };
- })();
- var myRevealingModule = (function () {
- var privateVar = "Ben Cherry",
- publicVar = "Hey there!";
- function privateFunction() {
- console.log( "Name:" + privateVar );
- }
- function publicSetName( strName ) {
- privateVar = strName;
- }
- function publicGetName() {
- privateFunction();
- }
- // Reveal public pointers to
- // private functions and properties
- return {
- setName: publicSetName,
- greeting: publicVar,
- getName: publicGetName
- };
- })();
- myRevealingModule.setName( "Paul Kinlan" );
- var yourNamespace = (function() {
- //Private property
- var publicScope = {};
- //Private property
- var privateProperty = "aaa";
- //Public property
- publicScope.publicProperty = "bbb";
- //Public method
- publicScope.publicMethod = function() {
- this.privateMethod();
- };
- //Private method
- function privateMethod() {
- console.log(this.privateProperty);
- }
- //Return only the public parts
- return publicScope;
- }());
- yourNamespace.publicMethod();
- var yourNamespace = {};
- yourNamespace.publicMethod = function() {
- // Do something...
- };
- yourNamespace.publicMethod2 = function() {
- // Do something...
- };
- yourNamespace.publicMethod();
- var namespace = function(name, separator, container){
- var ns = name.split(separator || '.'),
- o = container || window,
- i,
- len;
- for(i = 0, len = ns.length; i < len; i++){
- o = o[ns[i]] = o[ns[i]] || {};
- }
- return o;
- };
- var namespace=function(c,f,b){var e=c.split(f||"."),g=b||window,d,a;for(d=0,a=e.length;d<a;d++){g=g[e[d]]=g[e[d]]||{}}return g};
- namespace("com.example.namespace");
- com.example.namespace.test = function(){
- alert("In namespaced function.");
- };
- namespace("com.example.namespace").test = function(){
- alert("In namespaced function.");
- };
- com.example.namespace.test();
- const namespace = function(name, separator, container){
- var o = container || window;
- name.split(separator || '.').forEach(function(x){
- o = o[x] = o[x] || {};
- });
- return o;
- };
- var Namespace = new function() {
- var ClassFirst = this.ClassFirst = function() {
- this.abc = 123;
- }
- var ClassSecond = this.ClassSecond = function() {
- console.log("Cluttered way to access another class in namespace: ", new Namespace.ClassFirst().abc);
- console.log("Nicer way to access a class in same namespace: ", new ClassFirst().abc);
- }
- }
- var Namespace2 = new function() {
- var ClassFirst = this.ClassFirst = function() {
- this.abc = 666;
- }
- var ClassSecond = this.ClassSecond = function() {
- console.log("Cluttered way to access another class in namespace: ", new Namespace2.ClassFirst().abc);
- console.log("Nicer way to access a class in same namespace: ", new ClassFirst().abc);
- }
- }
- new Namespace.ClassSecond()
- new Namespace2.ClassSecond()
- Cluttered way to access another class in namespace: 123
- Nicer way to access a class in same namespace: 123
- Cluttered way to access another class in namespace: 666
- Nicer way to access a class in same namespace: 666
- (function ($, undefined) {
- console.log(this);
- }).call(window.myNamespace = window.myNamespace || {}, jQuery);
- var A = A|| {};
- A.B = {};
- A.B = {
- itemOne: null,
- itemTwo: null,
- };
- A.B.itemOne = function () {
- //..
- }
- A.B.itemTwo = function () {
- //..
- }
- // prelude.hjs
- billy = new (
- function moduleWrapper () {
- const exports = this;
- // postlude.hjs
- return exports;
- })();
- // someinternalfile.js
- function bob () { console.log('hi'); }
- exports.bob = bob;
- // clientfile.js
- billy.bob();
- while (true); do make; sleep 1; done
- Package("hello", [], function() {
- function greeting() {
- alert("Hello World!");
- }
- // Expose function greeting to other packages
- Export("greeting", greeting);
- });
- Package("example", ["hello"], function(greeting) {
- // Greeting is available here
- greeting(); // Alerts: "Hello World!"
- });
- function myObj() {
- this.prop1 = 1;
- this.prop2 = 2;
- this.prop3 = 'string';
- }
- var myObj = (
- (myObj instanceof Function !== false)
- ? Object.create({
- $props: new myObj(),
- fName1: function() { /* code.. */ },
- fName2: function() { /* code ...*/ }
- })
- : console.log('Object creation failed!')
- );
- function myObj() {
- this.prop1 = 1;
- this.prop2 = 2;
- this.prop3 = 'string';
- }
- var myObj = (
- (typeof(myObj) !== "function" || myObj instanceof Function === false)
- ? new Boolean()
- : Object.create({
- $props: new myObj(),
- init: function () { return; },
- fName1: function() { /* code.. */ },
- fName2: function() { /* code ...*/ }
- })
- );
- if (myObj instanceof Boolean) {
- Object.freeze(myObj);
- console.log('myObj failed!');
- debugger;
- }
- else
- myObj.init();
- //Register NameSpaces Function
- function registerNS(args){
- var nameSpaceParts = args.split(".");
- var root = window;
- for(var i=0; i < nameSpaceParts.length; i++)
- {
- if(typeof root[nameSpaceParts[i]] == "undefined")
- root[nameSpaceParts[i]] = new Object();
- root = root[nameSpaceParts[i]];
- }
- }
- registerNS("oodles.HomeUtilities");
- registerNS("oodles.GlobalUtilities");
- var $OHU = oodles.HomeUtilities;
- var $OGU = oodles.GlobalUtilities;
- var oodles = {
- "HomeUtilities": {},
- "GlobalUtilities": {}
- };
- $OHU.initialization = function(){
- //Your Code Here
- };
- $OHU.initialization();
Add Comment
Please, Sign In to add comment