Guest User

Untitled

a guest
Mar 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. if (Foo == null || typeof(Foo) != "object") { var Foo = new Object();}
  2.  
  3. var yourNamespace = {
  4.  
  5. foo: function() {
  6. },
  7.  
  8. bar: function() {
  9. }
  10. };
  11.  
  12. ...
  13.  
  14. yourNamespace.foo();
  15.  
  16. (function( skillet, $, undefined ) {
  17. //Private Property
  18. var isHot = true;
  19.  
  20. //Public Property
  21. skillet.ingredient = "Bacon Strips";
  22.  
  23. //Public Method
  24. skillet.fry = function() {
  25. var oliveOil;
  26.  
  27. addItem( "tn Butter nt" );
  28. addItem( oliveOil );
  29. console.log( "Frying " + skillet.ingredient );
  30. };
  31.  
  32. //Private Method
  33. function addItem( item ) {
  34. if ( item !== undefined ) {
  35. console.log( "Adding " + $.trim(item) );
  36. }
  37. }
  38. }( window.skillet = window.skillet || {}, jQuery ));
  39.  
  40. //Adding new Functionality to the skillet
  41. (function( skillet, $, undefined ) {
  42. //Private Property
  43. var amountOfGrease = "1 Cup";
  44.  
  45. //Public Method
  46. skillet.toString = function() {
  47. console.log( skillet.quantity + " " +
  48. skillet.ingredient + " & " +
  49. amountOfGrease + " of Grease" );
  50. console.log( isHot ? "Hot" : "Cold" );
  51. };
  52. }( window.skillet = window.skillet || {}, jQuery ));
  53.  
  54. var ns = new function() {
  55.  
  56. var internalFunction = function() {
  57.  
  58. };
  59.  
  60. this.publicFunction = function() {
  61.  
  62. };
  63. };
  64.  
  65. var your_namespace = your_namespace || {};
  66.  
  67. var your_namespace = your_namespace || {};
  68. your_namespace.Foo = {toAlert:'test'};
  69. your_namespace.Bar = function(arg)
  70. {
  71. alert(arg);
  72. };
  73. with(your_namespace)
  74. {
  75. Bar(Foo.toAlert);
  76. }
  77.  
  78. var MYNS = MYNS || {};
  79.  
  80. MYNS.subns = (function() {
  81.  
  82. function privateMethod() {
  83. // Do private stuff, or build internal.
  84. return "Message";
  85. }
  86.  
  87. return {
  88. someProperty: 'prop value',
  89. publicMethod: function() {
  90. return privateMethod() + " stuff";
  91. }
  92. };
  93. })();
  94.  
  95. var MYNS = MYNS || {};
  96.  
  97. MYNS.subns = (function() {
  98. var internalState = "Message";
  99.  
  100. var privateMethod = function() {
  101. // Do private stuff, or build internal.
  102. return internalState;
  103. };
  104. var publicMethod = function() {
  105. return privateMethod() + " stuff";
  106. };
  107.  
  108. return {
  109. someProperty: 'prop value',
  110. publicMethod: publicMethod
  111. };
  112. })();
  113.  
  114. namespace = window.namespace || {};
  115. namespace.namespace1 = namespace.namespace1 || {};
  116.  
  117. namespace.namespace1.doSomeThing = function(){}
  118.  
  119. namespace = window.namespace || {};
  120. namespace.namespace2 = namespace.namespace2 || {};
  121.  
  122. namespace.namespace2.doSomeThing = function(){}
  123.  
  124. /**
  125. * My JavaScript application
  126. *
  127. * @module myapp
  128. */
  129.  
  130. /** @namespace Namespace for MYAPP classes and functions. */
  131. var MYAPP = MYAPP || {};
  132.  
  133. /**
  134. * A maths utility
  135. * @namespace MYAPP
  136. * @class math_stuff
  137. */
  138. MYAPP.math_stuff = {
  139.  
  140. /**
  141. * Sums two numbers
  142. *
  143. * @method sum
  144. * @param {Number} a First number
  145. * @param {Number} b Second number
  146. * @return {Number} Sum of the inputs
  147. */
  148. sum: function (a, b) {
  149. return a + b;
  150. },
  151.  
  152. /**
  153. * Multiplies two numbers
  154. *
  155. * @method multi
  156. * @param {Number} a First number
  157. * @param {Number} b Second number
  158. * @return {Number} The inputs multiplied
  159. */
  160. multi: function (a, b) {
  161. return a * b;
  162. }
  163. };
  164.  
  165. /**
  166. * Constructs Person objects
  167. * @class Person
  168. * @constructor
  169. * @namespace MYAPP
  170. * @param {String} First name
  171. * @param {String} Last name
  172. */
  173. MYAPP.Person = function (first, last) {
  174.  
  175. /**
  176. * First name of the Person
  177. * @property first_name
  178. * @type String
  179. */
  180. this.first_name = first;
  181.  
  182. /**
  183. * Last name of the Person
  184. * @property last_name
  185. * @type String
  186. */
  187. this.last_name = last;
  188. };
  189.  
  190. /**
  191. * Return Person's full name
  192. *
  193. * @method getName
  194. * @return {String} First name + last name
  195. */
  196. MYAPP.Person.prototype.getName = function () {
  197. return this.first_name + ' ' + this.last_name;
  198. };
  199.  
  200. var myNamespace = {}
  201. myNamespace._construct = function()
  202. {
  203. var staticVariable = "This is available to all functions created here"
  204.  
  205. function MyClass()
  206. {
  207. // Depending on the class, we may build all the classes here
  208. this.publicMethod = function()
  209. {
  210. //Do stuff
  211. }
  212. }
  213.  
  214. // Alternatively, we may use a prototype.
  215. MyClass.prototype.altPublicMethod = function()
  216. {
  217. //Do stuff
  218. }
  219.  
  220. function privateStuff()
  221. {
  222. }
  223.  
  224. function publicStuff()
  225. {
  226. // Code that may call other public and private functions
  227. }
  228.  
  229. // List of things to place publically
  230. this.publicStuff = publicStuff
  231. this.MyClass = MyClass
  232. }
  233. myNamespace._construct()
  234.  
  235. // The following may or may not be in another file
  236. myNamespace.subName = {}
  237. myNamespace.subName._construct = function()
  238. {
  239. // Build namespace
  240. }
  241. myNamespace.subName._construct()
  242.  
  243. var myClass = new myNamespace.MyClass();
  244. var myOtherClass = new myNamepace.subName.SomeOtherClass();
  245. myNamespace.subName.publicOtherStuff(someParameter);
  246.  
  247. Namespace('my.awesome.package');
  248. my.awesome.package.WildClass = {};
  249.  
  250. Namespace('my.awesome.package', {
  251. SuperDuperClass: {
  252. saveTheDay: function() {
  253. alert('You are welcome.');
  254. }
  255. }
  256. });
  257.  
  258. var namespace = {};
  259. namespace.module1 = (function(){
  260.  
  261. var self = {};
  262. self.initialized = false;
  263.  
  264. self.init = function(){
  265. setTimeout(self.onTimeout, 1000)
  266. };
  267.  
  268. self.onTimeout = function(){
  269. alert('onTimeout')
  270. self.initialized = true;
  271. };
  272.  
  273. self.init(); /* If it needs to auto-initialize, */
  274. /* You can also call 'namespace.module1.init();' from outside the module. */
  275. return self;
  276. })()
  277.  
  278. function namespace(namespace) {
  279. var object = this, tokens = namespace.split("."), token;
  280.  
  281. while (tokens.length > 0) {
  282. token = tokens.shift();
  283.  
  284. if (typeof object[token] === "undefined") {
  285. object[token] = {};
  286. }
  287.  
  288. object = object[token];
  289. }
  290.  
  291. return object;
  292. }
  293.  
  294. // Usage example
  295. namespace("foo.bar").baz = "I'm a value!";
  296.  
  297. (function(){
  298.  
  299. namespace("images", previous, next);
  300. // ^^ This creates or finds a root object, images, and binds the two functions to it.
  301. // It works even though those functions are not yet defined.
  302.  
  303. function previous(){ ... }
  304.  
  305. function next(){ ... }
  306.  
  307. function find(){ ... } // A private function
  308.  
  309. })();
  310.  
  311. var MYNamespace = MYNamespace|| {};
  312.  
  313. MYNamespace.MyFirstClass = function (val) {
  314. this.value = val;
  315. this.getValue = function(){
  316. return this.value;
  317. };
  318. }
  319.  
  320. var myFirstInstance = new MYNamespace.MyFirstClass(46);
  321. alert(myFirstInstance.getValue());
  322.  
  323. global_namespace.Define('startpad.base', function(ns) {
  324. var Other = ns.Import('startpad.other');
  325. ....
  326. });
  327.  
  328. var myNamespace = (function () {
  329.  
  330. var myPrivateVar, myPrivateMethod;
  331.  
  332. // A private counter variable
  333. myPrivateVar = 0;
  334.  
  335. // A private function which logs any arguments
  336. myPrivateMethod = function( foo ) {
  337. console.log( foo );
  338. };
  339.  
  340. return {
  341.  
  342. // A public variable
  343. myPublicVar: "foo",
  344.  
  345. // A public function utilizing privates
  346. myPublicFunction: function( bar ) {
  347.  
  348. // Increment our private counter
  349. myPrivateVar++;
  350.  
  351. // Call our private method using bar
  352. myPrivateMethod( bar );
  353.  
  354. }
  355. };
  356.  
  357. })();
  358.  
  359. var myRevealingModule = (function () {
  360.  
  361. var privateVar = "Ben Cherry",
  362. publicVar = "Hey there!";
  363.  
  364. function privateFunction() {
  365. console.log( "Name:" + privateVar );
  366. }
  367.  
  368. function publicSetName( strName ) {
  369. privateVar = strName;
  370. }
  371.  
  372. function publicGetName() {
  373. privateFunction();
  374. }
  375.  
  376.  
  377. // Reveal public pointers to
  378. // private functions and properties
  379.  
  380. return {
  381. setName: publicSetName,
  382. greeting: publicVar,
  383. getName: publicGetName
  384. };
  385.  
  386. })();
  387.  
  388. myRevealingModule.setName( "Paul Kinlan" );
  389.  
  390. var yourNamespace = (function() {
  391.  
  392. //Private property
  393. var publicScope = {};
  394.  
  395. //Private property
  396. var privateProperty = "aaa";
  397.  
  398. //Public property
  399. publicScope.publicProperty = "bbb";
  400.  
  401. //Public method
  402. publicScope.publicMethod = function() {
  403. this.privateMethod();
  404. };
  405.  
  406. //Private method
  407. function privateMethod() {
  408. console.log(this.privateProperty);
  409. }
  410.  
  411. //Return only the public parts
  412. return publicScope;
  413. }());
  414.  
  415. yourNamespace.publicMethod();
  416.  
  417. var yourNamespace = {};
  418.  
  419. yourNamespace.publicMethod = function() {
  420. // Do something...
  421. };
  422.  
  423. yourNamespace.publicMethod2 = function() {
  424. // Do something...
  425. };
  426.  
  427. yourNamespace.publicMethod();
  428.  
  429. var namespace = function(name, separator, container){
  430. var ns = name.split(separator || '.'),
  431. o = container || window,
  432. i,
  433. len;
  434. for(i = 0, len = ns.length; i < len; i++){
  435. o = o[ns[i]] = o[ns[i]] || {};
  436. }
  437. return o;
  438. };
  439.  
  440. 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};
  441.  
  442. namespace("com.example.namespace");
  443. com.example.namespace.test = function(){
  444. alert("In namespaced function.");
  445. };
  446.  
  447. namespace("com.example.namespace").test = function(){
  448. alert("In namespaced function.");
  449. };
  450.  
  451. com.example.namespace.test();
  452.  
  453. const namespace = function(name, separator, container){
  454. var o = container || window;
  455. name.split(separator || '.').forEach(function(x){
  456. o = o[x] = o[x] || {};
  457. });
  458. return o;
  459. };
  460.  
  461. var Namespace = new function() {
  462. var ClassFirst = this.ClassFirst = function() {
  463. this.abc = 123;
  464. }
  465.  
  466. var ClassSecond = this.ClassSecond = function() {
  467. console.log("Cluttered way to access another class in namespace: ", new Namespace.ClassFirst().abc);
  468. console.log("Nicer way to access a class in same namespace: ", new ClassFirst().abc);
  469. }
  470. }
  471.  
  472. var Namespace2 = new function() {
  473. var ClassFirst = this.ClassFirst = function() {
  474. this.abc = 666;
  475. }
  476.  
  477. var ClassSecond = this.ClassSecond = function() {
  478. console.log("Cluttered way to access another class in namespace: ", new Namespace2.ClassFirst().abc);
  479. console.log("Nicer way to access a class in same namespace: ", new ClassFirst().abc);
  480. }
  481. }
  482.  
  483. new Namespace.ClassSecond()
  484. new Namespace2.ClassSecond()
  485.  
  486. Cluttered way to access another class in namespace: 123
  487. Nicer way to access a class in same namespace: 123
  488. Cluttered way to access another class in namespace: 666
  489. Nicer way to access a class in same namespace: 666
  490.  
  491. (function ($, undefined) {
  492.  
  493. console.log(this);
  494.  
  495. }).call(window.myNamespace = window.myNamespace || {}, jQuery);
  496.  
  497. var A = A|| {};
  498. A.B = {};
  499.  
  500. A.B = {
  501. itemOne: null,
  502. itemTwo: null,
  503. };
  504.  
  505. A.B.itemOne = function () {
  506. //..
  507. }
  508.  
  509. A.B.itemTwo = function () {
  510. //..
  511. }
  512.  
  513. // prelude.hjs
  514. billy = new (
  515. function moduleWrapper () {
  516. const exports = this;
  517.  
  518. // postlude.hjs
  519. return exports;
  520. })();
  521.  
  522. // someinternalfile.js
  523. function bob () { console.log('hi'); }
  524. exports.bob = bob;
  525.  
  526. // clientfile.js
  527. billy.bob();
  528.  
  529. while (true); do make; sleep 1; done
  530.  
  531. Package("hello", [], function() {
  532. function greeting() {
  533. alert("Hello World!");
  534. }
  535. // Expose function greeting to other packages
  536. Export("greeting", greeting);
  537. });
  538.  
  539. Package("example", ["hello"], function(greeting) {
  540. // Greeting is available here
  541. greeting(); // Alerts: "Hello World!"
  542. });
  543.  
  544. function myObj() {
  545. this.prop1 = 1;
  546. this.prop2 = 2;
  547. this.prop3 = 'string';
  548. }
  549.  
  550. var myObj = (
  551. (myObj instanceof Function !== false)
  552. ? Object.create({
  553.  
  554. $props: new myObj(),
  555. fName1: function() { /* code.. */ },
  556. fName2: function() { /* code ...*/ }
  557. })
  558. : console.log('Object creation failed!')
  559. );
  560.  
  561. function myObj() {
  562. this.prop1 = 1;
  563. this.prop2 = 2;
  564. this.prop3 = 'string';
  565. }
  566.  
  567. var myObj = (
  568. (typeof(myObj) !== "function" || myObj instanceof Function === false)
  569. ? new Boolean()
  570. : Object.create({
  571. $props: new myObj(),
  572. init: function () { return; },
  573. fName1: function() { /* code.. */ },
  574. fName2: function() { /* code ...*/ }
  575. })
  576. );
  577.  
  578. if (myObj instanceof Boolean) {
  579. Object.freeze(myObj);
  580. console.log('myObj failed!');
  581. debugger;
  582. }
  583. else
  584. myObj.init();
  585.  
  586. //Register NameSpaces Function
  587. function registerNS(args){
  588. var nameSpaceParts = args.split(".");
  589. var root = window;
  590.  
  591. for(var i=0; i < nameSpaceParts.length; i++)
  592. {
  593. if(typeof root[nameSpaceParts[i]] == "undefined")
  594. root[nameSpaceParts[i]] = new Object();
  595.  
  596. root = root[nameSpaceParts[i]];
  597. }
  598. }
  599.  
  600. registerNS("oodles.HomeUtilities");
  601. registerNS("oodles.GlobalUtilities");
  602. var $OHU = oodles.HomeUtilities;
  603. var $OGU = oodles.GlobalUtilities;
  604.  
  605. var oodles = {
  606. "HomeUtilities": {},
  607. "GlobalUtilities": {}
  608. };
  609.  
  610. $OHU.initialization = function(){
  611. //Your Code Here
  612. };
  613.  
  614. $OHU.initialization();
Add Comment
Please, Sign In to add comment