Prev: FYI: Creating circular references is a perfectly OK thing to do.
Next: Volunteers Requested for open source publishing project
From: Karl Tikjøb Krukow on 29 Jul 2010 01:51 On 29/07/10 07.45, Karl Tikj�b Krukow wrote: > On 26/07/10 14.26, Stanimir Stamenkov wrote: >> As the number of utility functions I write increases I've started to >> use a global variable acting as namespace for my functions. Given my >> functions are spread in couple of .js files and the order of loading >> is not significant (documents may include random combination of the >> files) I've wondered how it is best to initialize the namespace >> object. > > I've a general function, similar to the following in the past I've *used* a ... > > var namespace = (function() { > var globalObject = this; return function (/*String*/ spec) { > var i, part, context = globalObject; > while (true) { > i = spec.indexOf("."); > if (i < 0) { > part = spec; > } else { > part = spec.substring(0, i); > spec = spec.substring(i + 1); > } > context[part] = context[part] || {}; > context = context[part]; > if (i < 0) { > break; > } > } > return context; }; > })(); > > > //usage > namespace("App.Module").init = function(){ > var A = App, M = A.Module;//short names, fast lookup > //... > }; > > If wanted, we can define a "using" function > > namespace("App.Module"); > using(App.Module).run(function(m) { > //m refers to App.Module > }); > > > Karl.
From: Richard Cornford on 29 Jul 2010 07:04 On Jul 29, 3:16 am, Scott Sauyet wrote: <snip> > If the definition of "namespace" as commonly understood covers > exactly the sort of construct under question, then there is > little room for confusion. Unfortunately javascript tends to suffer from things that are commonly misunderstood. How many ES3 "bind" functions have you seen associating the word 'scope' with what will be the - this - value? That (or the underlying misconception(s)) hasn't helped people understand javascript's lexical scope or its runtime determined handling of - this - values. Indeed that particular misuse of terminology has occasionally gone as far as preventing people from actually asking the question that they wanted an answer to. > Why do you think it could cause problems? If you didn't > understand it, I am disagreeing entirely that there is no > such thing as a namespace in JS; I believe the construct > under question ("var MYNS = MYNS || {}") defines a > namesapce. Who would this confuse, and why? You would end up with people talking about 'javascript namespaces'; things that don't actually exist (at least in ES3, introducing the possibility that they are talking about JScript.net or something), when they wanted to be talking about 'namespaces' implemented with (or in) javascript (which certainly can exist). The distinction may seem pedantic but when the use of a few extra words can eliminate ambiguity then that justifies using those words. Richard.
From: Scott Sauyet on 29 Jul 2010 08:21 Richard Cornford wrote: > On Jul 29, 3:16 am, Scott Sauyet wrote: >> Why do you think it could cause problems? If you didn't >> understand it, I am disagreeing entirely that there is no >> such thing as a namespace in JS; I believe the construct >> under question ("var MYNS = MYNS || {}") defines a >> namesapce. Who would this confuse, and why? > > You would end up with people talking about 'javascript namespaces'; > things that don't actually exist (at least in ES3, introducing the > possibility that they are talking about JScript.net or something), > when they wanted to be talking about 'namespaces' implemented with (or > in) javascript (which certainly can exist). The distinction may seem > pedantic but when the use of a few extra words can eliminate ambiguity > then that justifies using those words. I wasn't arguing for the phrase "javascript namespaces", only for "namespaces" and since I never try to introduce a "namespace" function or treat "namespace" as a keyword, I don't see the potential for confusion. Perhaps that's just me being naive. My only recent point in this thread was to counter David Mark's assertion that: | They should also understand that there is no such thing as a | "namespace" in JS. It's a global variable referencing an object. With the way discussions often proceed around here, I'd be crazy to complain about a single instance of pedantry! :-) -- Scott
From: David Mark on 29 Jul 2010 09:02 On Jul 29, 8:21 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > Richard Cornford wrote: > > On Jul 29, 3:16 am, Scott Sauyet wrote: > >> Why do you think it could cause problems? If you didn't > >> understand it, I am disagreeing entirely that there is no > >> such thing as a namespace in JS; I believe the construct > >> under question ("var MYNS = MYNS || {}") defines a > >> namesapce. Who would this confuse, and why? > > > You would end up with people talking about 'javascript namespaces'; > > things that don't actually exist (at least in ES3, introducing the > > possibility that they are talking about JScript.net or something), > > when they wanted to be talking about 'namespaces' implemented with (or > > in) javascript (which certainly can exist). The distinction may seem > > pedantic but when the use of a few extra words can eliminate ambiguity > > then that justifies using those words. > > I wasn't arguing for the phrase "javascript namespaces", only for > "namespaces" and since I never try to introduce a "namespace" function > or treat "namespace" as a keyword, I don't see the potential for > confusion. Perhaps that's just me being naive. > > My only recent point in this thread was to counter David Mark's > assertion that: > > | They should also understand that there is no such thing as a > | "namespace" in JS. It's a global variable referencing an object. And as has been explained to you, you have no counter to that. JS (referring to ES3 of course) has no concept of namespaces. You may implement whatever you want in JS and call it whatever you want, but that doesn't make it part of JS. > > With the way discussions often proceed around here, I'd be crazy to > complain about a single instance of pedantry! :-) > So you were being pedantic and you'd be crazy to complain about it? I don't follow.
From: Scott Sauyet on 29 Jul 2010 10:00
David Mark wrote: > On Jul 29, 8:21 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: >> Richard Cornford wrote: >>> On Jul 29, 3:16 am, Scott Sauyet wrote: >>>> Why do you think it could cause problems? If you didn't >>>> understand it, I am disagreeing entirely that there is no >>>> such thing as a namespace in JS; I believe the construct >>>> under question ("var MYNS = MYNS || {}") defines a >>>> namesapce. Who would this confuse, and why? > >>> You would end up with people talking about 'javascript namespaces'; >>> things that don't actually exist [ ... ]. > >> I wasn't arguing for the phrase "javascript namespaces", only for >> "namespaces" and since I never try to introduce a "namespace" function >> or treat "namespace" as a keyword, I don't see the potential for >> confusion. Perhaps that's just me being naive. > >> My only recent point in this thread was to counter David Mark's >> assertion that: > >> | They should also understand that there is no such thing as a >> | "namespace" in JS. It's a global variable referencing an object. > > And as has been explained to you, you have no counter to that. JS > (referring to ES3 of course) has no concept of namespaces. You may > implement whatever you want in JS and call it whatever you want, but > that doesn't make it part of JS. Well. so much for your being done with this discussion, huh? :-) As already made abundantly clear to anyone who cares, the constructs under question are namespaces in JS under any reasonable definition of "namespace". No one ever suggested that there was some native language support for namespaces. >> With the way discussions often proceed around here, I'd be crazy to >> complain about a single instance of pedantry! :-) > > So you were being pedantic and you'd be crazy to complain about it? I > don't follow. You really need to learn to read before you respond. -- Scott |