Javascript Namespace

S

shapper

Hello,

How to create a namespace in Javascript containing two methods?
And how to access those methods?

Thanks,
Miguel
 
R

RobG

Hello,

How to create a namespace in Javascript

I presume you don't really mean XML namespace. :)

The term "namespace" in javascript is sometimes (probably erroneously)
used to describe a design pattern where functions are added as
properties of a single object rather than as global functions.

containing two methods?

Sounds a lot like homework to me. Create an object, add two
properties, assign them references to functions.

And how to access those methods?

The same way you access any object properties, use either dot or
square bracket notation as appropriate.

Have a go, post the result and (almost certainly) you'll get
constructive criticism.
 
R

RoLo

Hello,

How to create a namespace in Javascript containing two methods?
And how to access those methods?

Thanks,
Miguel

you mean this:

var name={
method1:function(){alert(1);},
method2:function(){alert(2);}
};

name.method1();
name.method2();
 
T

Thomas 'PointedEars' Lahn

RobG said:
I presume you don't really mean XML namespace. :)

The term "namespace" in javascript is sometimes (probably erroneously)
used to describe a design pattern where functions are added as
properties of a single object rather than as global functions.

So-called "global functions" are in fact methods of the Global Object, a
single object at that. Your statement is only beginning to make sense if
you say "single *user-defined* object" instead.


PointedEars
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in comp.lang.javascript:
So-called "global functions" are in fact methods of the Global Object, a
single object at that. Your statement is only beginning to make sense if
you say "single *user-defined* object" instead.

It would be beter to say "default object", or better "root default object",
rather than "global object", since this default is not a default
everywhere, but only where the local scope has no other default defined,
as it must be in the root.

[Global, in my view means everywhere, so also in local places.]

It is no matter whether a variable or an object is ment, as it is only the
named pointer that is affected. Only I do not think it is possible to
define a local scope for the default object?

<script type='text/javascript'>

var obj = {};

window.a = function(){alert('default-a')};
obj.a = function() {alert('obj-a')};

a();
window.a();
obj.a();

</script>
 
R

Richard Cornford

Evertjan said:
Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in
comp.lang.javascript:


It would be beter to say "default object", or better "root
default object", rather than "global object", since this
default is not a default everywhere, but only where the
local scope has no other default defined, as it must be in
the root.

I don't see that as better. It is getting pretty VKesque; I cannot tell
what you are talking about by reading the words.
[Global, in my view means everywhere, so also in local places.]

The global object is at the end of every scope chain, and so it is
everywhere (at least in some sense).
It is no matter whether a variable or an object is ment, as it
is only the named pointer that is affected. Only I do not think
it is possible to define a local scope for the default object?
<snip>

What do you mean by "default object " here? Is it the object used as the
'base' of the resulting Reference type when Identifier resolution fails
to find a corresponding property name on any object on the scope chain?
That would be the object labelled as "the global object" by the
language's specification.

Richard.
 
R

RobG

So-called "global functions" are in fact methods of the Global Object, a
single object at that. Your statement is only beginning to make sense if
you say "single *user-defined* object" instead.

The phrase "rather than" infers some other object than the global
object. I'm sorry if it is ambiguous.

The name space object could be any object other than the global
object, be it native or host. It need not be a user-defined native
object, though I would expect it to be.
 
E

Evertjan.

Richard Cornford wrote on 23 jun 2008 in comp.lang.javascript:
I don't see that as better. It is getting pretty VKesque; I cannot
tell what you are talking about by reading the words.

I'm sorry.
[Global, in my view means everywhere, so also in local places.]

The global object is at the end of every scope chain, and so it is
everywhere (at least in some sense).

Now you are just repeating the definition I just pointed to as wrong.
<snip>

What do you mean by "default object " here? Is it the object used as
the 'base' of the resulting Reference type when Identifier resolution
fails to find a corresponding property name on any object on the scope
chain?

Now I cannot tell what you are talking about by reading the words.
I think you are saying the same as I did in a complicated way.

The default object is just the object that is "assumed" if no definite
object is specified.

So:

window.myThing

points to the same variable/function/whatever as:

myThing

unless the local default object is specified otherwize.

So methods and values can have a default objects, while objects, methods
and values can have a default action/method/etc.

I do not think that that is completely implemented in JS, but that is a
nice goal for object oriented "namespace naming".
That would be the object labelled as "the global object" by the
language's specification.

Sure, but I object to the word "global" in such specification, as
"global" means everywhere, and not "everywhere unless"

In the above it just means "default" or "root", if the name space system
is seen as a tree. If it is internally a tree does [to me] not matter to
the naming.
 
L

Lasse Reichstein Nielsen

Evertjan. said:
Thomas 'PointedEars' Lahn wrote on 22 jun 2008 in comp.lang.javascript:


It would be beter to say "default object", or better "root default object",
rather than "global object", since this default is not a default
everywhere,

Default what?
but only where the local scope has no other default defined,
as it must be in the root.

As it must be *what* in the root?
[Global, in my view means everywhere, so also in local places.]

The global object is an object, no matter where you are. There must
be some property of this object that you think is not globally applicable.
It is no matter whether a variable or an object is ment, as it is only the
named pointer that is affected. Only I do not think it is possible to
define a local scope for the default object?

I just don't understand this. Try to explain it with more words.
(E.g., do you mean by "default object" what was previously in the thread
called "global object"?)

/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javascript:
Default what?

As I wrote in the parallel? posting:

The default object is just the object that is "assumed" if no definite
object is specified.

.... when calling that object in the script.

So, in browser js:

window.myValue is the same as myValue,
unless in a subscope myValue is redefined
[= has a pointer to another object/property/method]

It would be nice of each subscope could be given a named default subroot
object, because now only the myValue of the root scope's and the present
scope's myValue are callable, not the in-between-scope's myValue-s.
As it must be *what* in the root?

Object. [the default word in this sentence ;-)]
The same however is true for all defaulting.

[Global, in my view means everywhere, so also in local places.]

The global object is an object, no matter where you are.

Yes, just like any other object, but that's why naming that "global" is not
right. It is the root object of the root scope.

Theoretically you could set another object as the default object in the
root, so that the original root object is no longer the default.
[Perhaps not possible in present day JS, but that is only implementation.]

Seen as a tree, you could call the object the "global root object",
but why shorten that to "global object"? Beter shorten that to "the root
object"

Even the naming pointer to this object can be overwritten to mean another
object, either in the root scope or any other scope. This could make the
global root object effectively unreachable in that scope.
There must
be some property of this object that you think is not globally
applicable.

Why? If that is true the same must go for all other objects in the root
scope. But this is not about properties or methods, but about the object
itself.

Objects are only accessable in JS by their naming pointer. different scopes
can use the same name for different objects.

In the same sense there are no local objects, just local names pointing to
objects.

If an object is defined in a local scope, however, it would at present be
difficult to export the use of such object to another scope that is not a
"subscope" of that scope, just because there is no way to move the pointer
to that other scope.
I just don't understand this. Try to explain it with more words.
(E.g., do you mean by "default object" what was previously in the
thread called "global object"?)

No, the default object is the object that does not have to be named in that
scope. Perhaps I used that where I ment to say the root object. In JS, it
usualy is the same object, but that is implementation.

I just object to using the word global here for the root object,
becaus it gives the wrong idea, that it is the only object available in all
[sub]scopes. All other objects of [= defined in] the root scope have the
same possibilities, as long as the name is not reused.
 
L

Lasse Reichstein Nielsen

Evertjan. said:
Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javascript:
Default what?

As I wrote in the parallel? posting:

The default object is just the object that is "assumed" if no definite
object is specified.

... when calling that object in the script.

So, in browser js:

window.myValue is the same as myValue,
unless in a subscope myValue is redefined
[= has a pointer to another object/property/method]

The expression "myValue" is a variable expression. It resolves the
variable wrt. the current scope. In Javascript it is a lexical scope.
If no variable with that name exists in the scope, then Javascript
considers the variable to be a global variable. Global variables
are properties of the global object.

And "window.myValue" is a property lookup on the global object, if
"window" resolves to a reference to the global object.

As I read your explanation, it seems that you consider "myValue" as
being equivalent to "window.myValue" because it is equivalent to a
property lookup, with the global object as a "default object" where
"myValue" has no explicit object of the property.

I consider variable lookup and property lookup to be very different
processes. Naming an object that holds the global variables the
"global object" is perfectly reasonable to me, and calling it a
"default object" is misleading, because "myValue" is not treated
as a property lookup on a default object.
It would be nice of each subscope could be given a named default subroot
object, because now only the myValue of the root scope's and the present
scope's myValue are callable,

they are the only ones that can be accessed, yes.
not the in-between-scope's myValue-s.

Indeed. A lexical scope means that you can only access the closest
enclosing declaration of a given name. However, being lexically scoped,
whoever wrote it would have access to all the code of the scope, and
could rename one of the variables if the shadowing wasn't intentional.

Yes, just like any other object, but that's why naming that "global" is not
right. It is the root object of the root scope.

Technically the global object is not in the scope chain. It is
accessed only when the scope chain fails to resolve a variable
name. Effectively, though, it is almost as if the global object was
at the root of all scope chains.
Theoretically you could set another object as the default object in the
root, so that the original root object is no longer the default.
[Perhaps not possible in present day JS, but that is only implementation.]

Even in present day Javascript, if you access a function defined in another
page, it will have a different global object.
Seen as a tree, you could call the object the "global root object",
but why shorten that to "global object"? Beter shorten that to "the root
object"

It's a matter of intent, I guess. The global object holds the global
variables. A root object would be at the root of the scope chain.
The authors of Javascript had the former perspective.

Even the naming pointer to this object can be overwritten to mean another
object, either in the root scope or any other scope. This could make the
global root object effectively unreachable in that scope.

The property "window" of the global object can probably be changed, or
shadowed by a local variable. However the global object is always accessible,
e.g.,
var global = (function(){return this;})();

Objects are only accessable in JS by their naming pointer. different scopes
can use the same name for different objects.

Objects are passed around as references. Those references can be
stored in variables or object properties ("naming pointer"), but they
can also be the return value of a function.
In the same sense there are no local objects, just local names pointing to
objects.

There are no local objects. The only thing that is local (to a
function call) are local variables. That includes arguments
(also accessible as properties of the arguments object).
If an object is defined in a local scope, however, it would at present be
difficult to export the use of such object to another scope that is not a
"subscope" of that scope, just because there is no way to move the pointer
to that other scope.

There are two ways to have values exit a function call: store it in a
place that is also accessible outside the call (global variable or
local variable in an enclosing scope) or return it as the result
of the function. The latter requires no variables/names.
No, the default object is the object that does not have to be named in that
scope.

That's one view of what happens when you access a global variable.
However, that view is flawed as the expression "myVariable" is only
relative to the global object if it is not a local variable in scope.
It's not a property lookup where the target object "isn't named".
Perhaps I used that where I ment to say the root object. In JS, it
usualy is the same object, but that is implementation.

It's specification. The ECMAScript specification states that the
global object is where unresolved variables are finally attempted
resolved against. I.e., it's what you call the root object of scope
based variable resolution.
It is also the variables object of global code (code not inside a
function body).
I just object to using the word global here for the root object,
becaus it gives the wrong idea,

And I think it is the right idea.
that it is the only object available in all [sub]scopes.

That's not what "global object" means. But it is true that it is
accessible in all scopes. If a function is called directly, and not as
a method (referenced as "object.property"), then "this" refers to the
global object in the method body.
All other objects of [= defined in] the root scope have the
same possibilities, as long as the name is not reused.

Yes, that's just basic scope rules.

/L
 
H

Henry

Evertjan writes:

Technically the global object is not in the scope chain. It is
accessed only when the scope chain fails to resolve a variable
name. Effectively, though, it is almost as if the global object was
at the root of all scope chains.
<snip>
ECMA 262:-
| 10.2 Entering An Execution Context
|
| ...
| 10.2.1 Global Code
|
| The scope chain is created and initialised to contain the global
| object and no others.
|
| Variable instantiation is performed using the global object as
| the variable object and using property attributes {DontDelete }.
|
| The this value is the global object.

So in the global execution context the scope chain consists of one
object; the global object.

| 13 Function Definition
| ...
| The production FunctionDeclaration :
| function Identifier ( FormalParameterListopt ) { FunctionBody }
| is processed for function declarations as follows:
|
| 1. Create a new Function object as specified in section 13.2 with
| parameters specified by FormalParameterList, and body specified
| by FunctionBody. Pass in the scope chain of the running execution
| context as the Scope.
| ...

Note the "Pass in the scope chain of the running execution context as
the Scope". The same step appears in all function creation algorithms
except for the - new Function -.

| 13.2 Creating Function Objects
| Given an optional parameter list specified by FormalParameterList, a
| body specified by FunctionBody, and a scope chain specified by
Scope,
| a Function object is constructed as follows:
|
| 1. If ...
| ...
| 19. Set the [[Scope]] property of F to a new scope chain (section
| 10.1.4) that contains the same objects as Scope.
| 20. Return F.

The "Scope" passed becomes the [[Scope]] of the new function object,
and then with a Variable object added becomes the scope of the
execution context when the function is executed, and so the "running
execution context" if any inner functions get created. Thus the global
object *is* at the end of all scope chains in javascript.
 
T

Thomas 'PointedEars' Lahn

RobG said:
Thomas said:
So-called "global functions" are in fact methods of the Global Object, a
single object at that. Your statement is only beginning to make sense if
you say "single *user-defined* object" instead.

[...]
The name space object could be any object other than the global
object, be it native or host. It need not be a user-defined native
object, though I would expect it to be.

With host objects, all bets are off.[tm]

So it should be a native object. Since the Global Object cannot be
considered (using it created the problem in the first place) and other
built-in objects do not allow arbitrary augmentation (consider built-in
properties with the ReadOnly attribute), it needs to be a user-defined object.


PointedEars
 
L

Lasse Reichstein Nielsen

Henry said:
Thus the global object *is* at the end of all scope chains in
javascript.

Indeed, my bad.

Note to self: Check spec before presseing "send".
/L
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javascript:
In Javascript it is a lexical scope.

Lasse, we are speaking in different worlds.

You are starting with what the original JS writing fathers or the ECMA
writers wanted it to be.

I am starting with what I would like to be a language of just objects
with it's properties and methods. An intellectual challenge.

All thsi started in this thread because Pointed put my Ears up on the
idea,
that a function must be nothing more or less than a method of an object,
usually the root object [my wording], and a variable a property of the
same. [while in fact they all thre are just pointers to those three, and
only those pointers 'have' scope limitations.]

The internal implementation, nor the primordial ideas of the writing
fathers come into play with my idealistic 'scope', where there is no
place for things like a "lexical" scope [what is that anyway?], and there
is no place to call anything global which is only the root of all
objects, as global is about scope, so only about pointers.

Probably even object hierarchy is only about pointers, I have to think
that through.

The present differences of use of object, object.method[function] and
object.property[variable] would be finished.

Defaulting would be only a matter of coding ease, not linked to any
internal substance but to scope limitations.

Oh what a wonderful objective Pointed pointer world it would be,
where not only the Ears, but the whole JS is "doing it with" pointers.

<http://www.audiobooksonline.com/media/1572701439.jpg>

;-)
 
L

Lasse Reichstein Nielsen

Evertjan. said:
Lasse Reichstein Nielsen wrote on 23 jun 2008 in comp.lang.javascript:


Lasse, we are speaking in different worlds.

You are starting with what the original JS writing fathers or the ECMA
writers wanted it to be.

I am starting with what I would like to be a language of just objects
with it's properties and methods. An intellectual challenge.

We are indeed speaking about different things, if you are talking
about a hypothetical language that is similar, but not equivalent, to
Javascript.
All thsi started in this thread because Pointed put my Ears up on the
idea,
that a function must be nothing more or less than a method of an object,
usually the root object [my wording], and a variable a property of the
same. [while in fact they all thre are just pointers to those three, and
only those pointers 'have' scope limitations.]

PointedEars merely pointed out that a "global function" was already a
property of an object.
Not all functions are, in Javascript.
Take this expression:

alert( (function(y){return function(x){return y+x};})(24)(18) );

There are two functions here that are not properties of any object
at any time. The outer is just an anonymous function. The inner
is an anonymous function that even leaves its defining scope.

The internal implementation, nor the primordial ideas of the writing
fathers come into play with my idealistic 'scope', where there is no
place for things like a "lexical" scope [what is that anyway?],

A lexical scope is one where the association between variable declaration
and variable use is determined by the program syntax. A variable use
always refer to a variable declaration in a syntactically enclosing
block/scope.

Example:
function test(x) {
var a = function(f,x) { return f(x); }
return a(function(y){return x;}, 42);
}
alert(test(37));

This alerts 37. The "x" in "return x" refers to the argument of the
"t" function, even if the function containing the "return x" is
eventually called in the scope of another declaration of "x".

Syntactic scope is so natural to most programmers that they don't even
think of it. However, not all languages have syntactic scope. The
Common Lisp and Emacs Lisp languages has the "opposite": dynamic scope.
Example(in elisp):
(let ((test (lambda (x)
(let ((a (lambda (f x) (funcall f x))))
(funcall a (lambda (y) x) 37)))))
(funcall test 42))

It is equivalent to the above Javascript program, but its result
is 37.
and there is no place to call anything global which is only the root
of all objects

Root of objects? Objects does not have to form any hierarchy. The
scope chains might, but if that is what you refer to, do call the
it "scope".
, as global is about scope, so only about pointers.

And I'm not sure what "pointers" mean here either.
Probably even object hierarchy is only about pointers, I have to think
that through.

Objects are referenced through references. Calling them pointers are
reasonable, although sometimes slightly misleading. But that means that
any object structure is connected through references, whether hierarchial
or not.
The present differences of use of object, object.method[function] and
object.property[variable] would be finished.

Is there a difference?

Personally, I'd much rather that they made the global object unreachable
through code, so the only way to create new variables is using the
"var" construction.
Accessing variables as properties of an object is so ... explicit.
It exposes what I consider an implementation detail: that an object
holds the variables. Most other languages do fine without that.

/L
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:
Example(in elisp):
(let ((test (lambda (x)
(let ((a (lambda (f x) (funcall f x))))
(funcall a (lambda (y) x) 37)))))
(funcall test 42))

It is equivalent to the above Javascript program, but its result
is 37.

Which is the correct result, when you noticed that I managed to swap
37 and 42 in the program, compared to the Javascript example.
Oops.
The point stands, though :)
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top