tag:blogger.com,1999:blog-865923359735383241.post2645653358873951149..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Javascript Namespace DeclarationUnknownnoreply@blogger.comBlogger18125tag:blogger.com,1999:blog-865923359735383241.post-64105724728590782602012-06-03T20:23:48.919-06:002012-06-03T20:23:48.919-06:00If anyone find this interesting,
var namespace =...If anyone find this interesting, <br /><br />var namespace = {};<br />namespace.module1 = (function(){<br /><br /> var self = {};<br /> self.initialized = false;<br /><br /> self.init = function(){<br /> setTimeout(self.onTimeout, 1000)<br /> };<br /><br /> self.onTimeout = function(){<br /> alert('onTimeout')<br /> self.initialized = true;<br /> };<br /><br /> self.init(); /* if it need to auto-initialize, */<br /> /* you can also call 'namespace.module1.init();' from outside the module */<br /> return self;<br /><br />})()<br /><br /><br />You can optionally declare a local variable same like self and assign local.onTimeout if you want it to be private.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-80499513759876097612012-06-03T20:23:48.257-06:002012-06-03T20:23:48.257-06:00You can declare a simple function to providing nam...You can declare a simple function to providing namespaces.<br /><br />function namespace(namespace) {<br /> var object = this, tokens = namespace.split("."), token;<br /><br /> while (tokens.length > 0) {<br /> token = tokens.shift();<br /><br /> if (typeof object[token] === "undefined") {<br /> object[token] = {};<br /> }<br /><br /> object = object[token];<br /> }<br /><br /> return object;<br />}<br /><br />// Usage example<br />namespace("foo.bar").baz = "I'm a value!";Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-57269023162511336252012-06-03T20:23:46.973-06:002012-06-03T20:23:46.973-06:00YUI has a YUI.namespace function to do thisYUI has a YUI.namespace function to do thisUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-31149410763065606382012-06-03T20:23:46.158-06:002012-06-03T20:23:46.158-06:00If using a Makefile you can do this.
// prelude.h...If using a Makefile you can do this.<br /><br />// prelude.hjs<br />billy = new (<br /> function moduleWrapper () {<br /> const exports = this;<br /><br />// postlude.hjs<br />return exports;<br />})();<br /><br />// someinternalfile.js<br />function bob () { console.log('hi'); }<br />exports.bob = bob;<br /><br />// clientfile.js<br />billy.bob();<br /><br /><br />I prefer to use a Makefile anyway once I get to about 1000 lines because I can effectively comment out large swaths of code by removing a single line in the makefile. It makes it easy to fiddle with stuff. Also, with this technique the namespace only appears once in the prelude so it's easy to change and you don't have to keep repeating it inside the library code.<br /><br />A shell script for live development in the browser when using a makefile:<br /><br />while (true); do make; sleep 1; done<br /><br /><br />Add this as a make task 'go' and you can 'make go' to keep your build updated as you code.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-27501722345747454262012-06-03T20:23:45.275-06:002012-06-03T20:23:45.275-06:00I've written another namespacing library that ...I've written another namespacing library that works a bit more like packages / units do in other languages. It allows you to create a package of Javascript code and the reference that package from other code:<br /><br />hello.js<br /><br />Package("hello", [], function() {<br /> function greeting() {<br /> alert("Hello World!");<br /> }<br /> // expose function greeting to other packages<br /> Export("greeting", greeting);<br />});<br /><br /><br />example.js<br /><br />Package("example", ["hello"], function(greeting) {<br /> // greeting is available here<br /> greeting(); // Alerts: "Hello World!"<br />});<br /><br /><br />Only the second file needs to be included in the page its dependencies (hello.js in this example) will automatically be loaded and the objects exported from those dependencies will be used to populate the arguments of the callback function.<br /><br />You can find the related project here:<br />Packages JSUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-24396880564345500602012-06-03T20:23:43.757-06:002012-06-03T20:23:43.757-06:00After porting several of my libraries to different...After porting several of my libraries to different projects, and having to constantly be changing the top level (statically named) namespace, I've switched to using this small (open source) helper function for defining namespaces.<br /><br />global_namespace.Define('startpad.base', function(ns) {<br /> var Other = ns.Import('startpad.other');<br /> ....<br />});<br /><br /><br />Description of the benefits are at my blog post. You can grab the source code here.<br /><br />One of the benefits I really like is isolation between modules with respect to load order. You can refer to an external module BEFORE it is loaded. And the object reference you get will be filled in when the code is available.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-32042640525728457142012-06-03T20:23:42.232-06:002012-06-03T20:23:42.232-06:00Here's how Stoyan Stefanov does it in his Java...Here's how Stoyan Stefanov does it in his JavaScript Patterns book which I to be found very good. (Also shows how he does comments that allows for auto generated API documentation, and how to add a method to a custom object's prototype):<br /><br />/**<br />* My js app<br />*<br />* @module myapp<br />*/<br /><br />var MYAPP = {};<br /><br />/**<br />* A maths utility<br />* @namespace MYAPP<br />* @class math_stuff<br />*/<br />MYAPP.math_stuff = {<br /><br /> /**<br /> * Sums 2 numbers<br /> *<br /> * @method sum<br /> * @param {Number} a First number<br /> * @param {Number} b Second number<br /> * @return {Number} Sum of the inputs<br /> */<br /> sum: function (a, b) {<br /> return a + b;<br /> },<br /><br /> /**<br /> * Multiplies 2 numbers<br /> *<br /> * @method multi<br /> * @param {Number} a First number<br /> * @param {Number} b Second number<br /> * @return {Number} The inputs multiplied<br /> */<br /> multi: function (a, b) {<br /> return a * b;<br /> }<br />};<br /><br />/**<br />* Constructs Person objects<br />* @class Person<br />* @constructor<br />* @namespace MYAPP<br />* @param {String} First name<br />* @param {String} Last name<br />*/<br />MYAPP.Person = function (first, last) {<br /><br /> /**<br /> * First name of the Person<br /> * @property first_name<br /> * @type String<br /> */<br /> this.first_name = first;<br /><br /> /**<br /> * Last name of the Person<br /> * @property last_name<br /> * @type String<br /> */<br /> this.last_name = last;<br />};<br /><br />/**<br />* Return Person's full name<br />*<br />* @method getName<br />* @return {String} First name + last name<br />*/<br />MYAPP.Person.prototype.getName = function () {<br /> return this.first_name + ' ' + this.last_name;<br />};Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-84247796590787724502012-06-03T20:23:41.089-06:002012-06-03T20:23:41.089-06:00http://github.com/smith/namespacedotjs
You gotta ...http://github.com/smith/namespacedotjs<br /><br />You gotta check that out!! :DUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-84905593561959768132012-06-03T20:23:39.721-06:002012-06-03T20:23:39.721-06:00I created namespace which is inspired by Erlang...I created namespace which is inspired by Erlang's modules. It is a very functional approach, but that is is how I write my js these days.<br /><br />It gives a closure a global namespace and exposes a defined set functions within that closure.<br /><br />(function(){<br /><br /> namespace("images", previous, next);<br /> // ^^ this creates or finds a root object, images, and binds the two functions to it.<br /> // It works even though those functions are not yet defined.<br /><br /> function previous(){ ... }<br /><br /> function next(){ ... }<br /><br /> function find(){ ... } // a private function<br /><br />})();Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-61820636593181809962012-06-03T20:23:38.042-06:002012-06-03T20:23:38.042-06:00or try this approach:
http://weblogs.asp.net/msch...or try this approach:<br /><br />http://weblogs.asp.net/mschwarz/archive/2005/08/26/423699.aspxUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-72481024061011543762012-06-03T20:23:37.406-06:002012-06-03T20:23:37.406-06:00I normally build it in a closure:
var MYNS = MYNS...I normally build it in a closure:<br /><br />var MYNS = MYNS || {};<br /><br />MYNS.subns = (function() {<br /><br /> function privateMethod() {<br /> // Do private stuff, or build internal.<br /> return "Message";<br /> }<br /><br /> return {<br /> someProperty: 'prop value',<br /> publicMethod: function() {<br /> return privateMethod() + " stuff";<br /> }<br /> };<br />})();Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-52364004714201057892012-06-03T20:23:36.243-06:002012-06-03T20:23:36.243-06:00Because you may write different files of javascrip...Because you may write different files of javascript and later combine or not combine them in an application each needs to be able to recover or construct the namespace object without damaging the work of other files...<br /><br />One file might intend to use the namespace namespace.namespace1<br /><br />namespace = window.namespace || {};<br />namespace.namespace1 = namespace.namespace1 || {};<br /><br />namespace.namespace1.doSomeThing = function(){}<br /><br /><br />Another file might want to use the namespace namespace.namespace2<br /><br />namespace = window.namespace || {};<br />namespace.namespace2 = namespace.namespace2 || {};<br /><br />namespace.namespace2.doSomeThing = function(){}<br /><br /><br />These two files can live together or apart without colliding.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-18548723173864955492012-06-03T20:23:33.184-06:002012-06-03T20:23:33.184-06:00I use this approach:-
var myNamespace = {}
myName...I use this approach:-<br /><br />var myNamespace = {}<br />myNamespace._construct = function()<br />{<br /> var staticVariable = "This is available to all functions created here"<br /><br /> function MyClass()<br /> {<br /> //Depending on the class may build all the class here<br /> this.publicMethod = function()<br /> {<br /> //Do stuff<br /> }<br /> }<br /> //Alternatively may use prototype<br /> MyClass.prototype.altPublicMethod = function()<br /> {<br /> //Do stuff<br /> }<br /><br /> function privateStuff()<br /> {<br /> }<br /><br /> function publicStuff()<br /> {<br /> //code that may call other public and private functions<br /> }<br /><br /> //List of things to place publically<br /> this.publicStuff = publicStuff<br /> this.MyClass = MyClass<br />}<br />myNamespace._construct()<br /><br />//The following may or may not be in another file<br />myNamespace.subName = {}<br />myNamespace.subName._construct = function()<br />{<br /> //build namespace<br />}<br />myNamespace.subName._construct()<br /><br /><br />External code can then:-<br /><br />var myClass = new myNamespace.MyClass();<br />var myOtherClass = new myNamepace.subName.SomeOtherClass();<br />myNamespace.subName.publicOtherStuff(someParameter);Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-48235632287280675612012-06-03T20:23:32.101-06:002012-06-03T20:23:32.101-06:00This is a follow-up to user106826's link to Na...This is a follow-up to user106826's link to Namespace.js. It seems the project moved to github. The new link is now:<br /><br />http://github.com/smith/namespacedotjs<br /><br />I have been using this simple js helper for my tiny project and so far it seems to be light yet versatile enough to handle namespacing and loading modules/classes. It would be great if it would allow me to import a package into a namespace of my choice, not just the global namespace... sigh, but that's besides the point.<br /><br />It allows you to declare the namespace then define objects/modules in that namespace:<br /><br />Namespace('my.awesome.package'); <br />my.awesome.package.WildClass = {};<br /><br /><br />Another option is to declare the namespace and it's contents at once:<br /><br />Namespace('my.awesome.package', {<br /> SuperDuperClass: {<br /> saveTheDay: function() {<br /> alert('You are welcome.');<br /> }<br /> }<br />});<br /><br /><br />For more usage examples, look at the example.js file in the source: http://github.com/smith/namespacedotjs/blob/master/example/sandbox.jsUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-71817719468636724622012-06-03T20:23:31.083-06:002012-06-03T20:23:31.083-06:00Is there a more elegant or succinct way of doing t...Is there a more elegant or succinct way of doing this?<br /><br /><br />yes it is:<br /><br />var your_namespace = your_namespace || {};<br /><br /><br />then you can have <br /><br />var your_namespace = your_namespace || {};<br />your_namespace.Foo = {toAlert:'test'};<br />your_namespace.Bar = function(arg) <br />{<br /> alert(arg);<br />};<br />with(your_namespace)<br />{<br /> Bar(Foo.toAlert);<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-20454786401677448612012-06-03T20:23:30.266-06:002012-06-03T20:23:30.266-06:00I use the approach found on the Enterprise jQuery ...I use the approach found on the Enterprise jQuery site, here <br /><br />Here is their example showing how to declare private & public properties and functions. Everything is done as a self-executing anonymous function.<br /><br />(function( skillet, $, undefined ) {<br /> //Private Property<br /> var isHot = true;<br /><br /> //Public Property<br /> skillet.ingredient = "Bacon Strips";<br /><br /> //Public Method<br /> skillet.fry = function() {<br /> var oliveOil;<br /><br /> addItem( "\t\n Butter \n\t" );<br /> addItem( oliveOil );<br /> console.log( "Frying " + skillet.ingredient );<br /> };<br /><br /> //Private Method<br /> function addItem( item ) {<br /> if ( item !== undefined ) {<br /> console.log( "Adding " + $.trim(item) );<br /> }<br /> } <br />}( window.skillet = window.skillet || {}, jQuery ));<br /><br /><br />So if you want to access one of the public members you would just go skillet.fry() or skillet.ingredients<br /><br />What's really cool is that you can now extend the namespace using the exact same syntax.<br /><br />//Adding New Functionality to the Skillet<br />(function( skillet, $, undefined ) {<br /> //Private Property<br /> var amountOfGrease = "1 Cup";<br /><br /> //Public Method<br /> skillet.toString = function() {<br /> console.log( skillet.quantity + " " + <br /> skillet.ingredient + " & " + <br /> amountOfGrease + " of Grease" );<br /> console.log( isHot ? "Hot" : "Cold" );<br /> }; <br />}( window.skillet = window.skillet || {}, jQuery ));Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-85200433233920073022012-06-03T20:23:29.248-06:002012-06-03T20:23:29.248-06:00Another way to do it, which I consider it to be a ...Another way to do it, which I consider it to be a little bit less restrictive than the object literal form, is this:<br /><br />var ns = new function() {<br /><br /> var internalFunction = function() {<br /><br /> };<br /><br /> this.publicFunction = function() {<br /><br /> };<br />};<br /><br /><br />The above is pretty much like the module pattern and whether you like it or not, it allows you to expose all your functions as public, while avoiding the rigid structure of an object literal.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-82444942790084708702012-06-03T20:23:28.488-06:002012-06-03T20:23:28.488-06:00I like this:
var yourNamespace = {
foo: func...I like this:<br /><br />var yourNamespace = {<br /><br /> foo: function() {<br /> },<br /><br /> bar: function() {<br /> }<br />};<br /><br />...<br /><br />yourNamespace.foo();Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com