1/** 2 * Namespace implementation for use with jQuery; adapted from Listing 6.16 from 3 * <em>Test-Driven JavaScript development</em>. 4 * 5 * <blockquote> 6 * 7 * <pre> 8 * var foo = $.namespace('foo'); 9 * foo.bar = function() {...} 10 * foo.baz = {...} 11 * </pre> 12 * 13 * </blockquote> 14 * 15 * The implementation can also be used to create separate custom namespaces: 16 * 17 * <blockquote> 18 * 19 * <pre> 20 * var custom = { 21 * namespace : $.namespace 22 * }; 23 * </pre> 24 * 25 * </blockquote> 26 * 27 * In this example, <code>custom.namespace</code> and <code>$.namespace</code> 28 * remain separate and distinct. 29 * 30 * @param namespaceString 31 * {string} the namespace to retrieve or create. Nested namespaces 32 * should be delineated with '.', for example 'outer.inner'. 33 * @returns {object} the resulting namespace, which is created if it does not 34 * already exist 35 */ 36jQuery.namespace = function(namespaceString) { 37 var object = window; 38 var levels = namespaceString.split("."); 39 40 for ( var i = 0, l = levels.length; i < l; i++) { 41 object[levels[i]] = object[levels[i]] || {}; 42 object = object[levels[i]]; 43 } 44 45 return object; 46}; 47 48/** 49 * Factory to create custom namespaces. 50 * 51 * <blockquote> 52 * 53 * <pre> 54 * var app = $.namespace.create(); 55 * app.namespace("foo"); 56 * app.foo.myObject = {} 57 * ... 58 * </pre> 59 * 60 * </blockquote> 61 * 62 * @returns {object} the new namespace 63 */ 64jQuery.namespace.create = function () { 65 return { 66 namespace : $.namespace 67 }; 68};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.