diff --git a/object-creation-patterns/module.html b/object-creation-patterns/module.html index 9bb8623..6d8e880 100644 --- a/object-creation-patterns/module.html +++ b/object-creation-patterns/module.html @@ -32,95 +32,39 @@ return parent; }; - MYAPP.namespace('MYAPP.utilities.Array'); - - MYAPP.utilities.array = (function () { - // private properties - var array_string = "[object Array]", - ops = Object.prototype.toString, + (function () { + MYAPP.namespace('MYAPP.utilities.array'); + MYAPP.utilities.Array = function () { + // private properties + var array_string = "[object Array]", + ops = Object.prototype.toString; // private methods - inArray = function (haystack, needle) { + var inArray = function (haystack, needle) { for (var i = 0, max = haystack.length; i < max; i += 1) { if (haystack[i] === needle) { return i; } } return -1; - }, - isArray = function (a) { - return ops.call(a) === array_string; }; - // end var - - // revealing public API - return { - isArray:isArray, - indexOf:inArray - }; - }()); - - - // Modules That Create Constructors - - var MYAPP = MYAPP || {}; - - MYAPP.namespace = function (ns_string) { - var parts = ns_string.split('.'), - parent = MYAPP, - i; - - // strip redundant leading global - if (parts[0] === "MYAPP") { - parts = parts.slice(1); - } - - for (i = 0; i < parts.length; i += 1) { - // create a property if it doesn't exist - if (typeof parent[parts[i]] === "undefined") { - parent[parts[i]] = {}; - } - parent = parent[parts[i]]; - } - return parent; - }; - - MYAPP.namespace('MYAPP.utilities.Array'); - MYAPP.utilities.Array = (function () { - // dependencies - var uobj = MYAPP.utilities.object, - ulang = MYAPP.utilities.lang, - - // private properties and methods... - Constr; + var isArray = function (a) { + return ops.call(a) === array_string; + }; - // end var - // optionally one-time init procedures - // ... - // public API -- constructor - Constr = function (o) { - this.elements = this.toArray(o); - }; - // public API -- prototype - Constr.prototype = { - constructor:MYAPP.utilities.Array, - version:"2.0", - toArray:function (obj) { - for (var i = 0, a = [], len = obj.length; i < len; i += 1) { - a[i] = obj[i]; - } - return a; - } - }; + // revealing public API + return { + isArray:isArray, + indexOf:inArray + }; + }; + })(); - // return the constructor - // to be assigned to the new namespace - return Constr; - }()); - var arr = new MYAPP.utilities.Array(obj); + var arr = new MYAPP.utilities.Array(); + console.log(arr.isArray({})); \ No newline at end of file