473,804 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Aliasing "namespaces " in JavaScript--why do we need to import symbols into a new namespace?

Ray
Hello,

I'm reading Mr. Flanagan's JS Definitive Guide 5th edition. I was
wondering about a point he make in section 10.2 of the book: "Importing
Symbols from Namespaces".

He mentions in there that one can create an alias for the symbol
defined in a namespace like this:

// This is an easier name, to save typing.
var define = com.davidflanag an.Class.define ;

Another way of doing it, again from the same section, is discussed
next:

// Create a simple namespace. No error checking required. The
// module user knows that this symbol does not exist yet.
var Class = {};
// Now import a symbol into this new namespace.
Class.define = com.davidflanag an.Class.define ;

Now my question is: what's wrong with aliasing the namespace itself?
E.g.:

var cls = com.davidflanag an.Class;
cls.define();

Why does he need to create a new simple namespace and import the symbol
from one namespace to another? Am I missing something really obvious
here?

Thanks,
Ray

Jan 27 '07 #1
1 4294
On Jan 26, 9:35 pm, "Ray" <ray_use...@yah oo.comwrote:
Hello,

I'm reading Mr. Flanagan's JS Definitive Guide 5th edition. I was
wondering about a point he make in section 10.2 of the book: "Importing
Symbols from Namespaces".

He mentions in there that one can create an alias for the symbol
defined in a namespace like this:

// This is an easier name, to save typing.
var define = com.davidflanag an.Class.define ;

Another way of doing it, again from the same section, is discussed
next:

// Create a simple namespace. No error checking required. The
// module user knows that this symbol does not exist yet.
var Class = {};
// Now import a symbol into this new namespace.
Class.define = com.davidflanag an.Class.define ;

Now my question is: what's wrong with aliasing the namespace itself?
E.g.:

var cls = com.davidflanag an.Class;
cls.define();

Why does he need to create a new simple namespace and import the symbol
from one namespace to another? Am I missing something really obvious
here?
It looks like he wants to protect the original namespace. For example

var com = {davidflanagan: {Class:{}}};
com.davidflanag an.Class.foo = function() {alert('foo');} ;
var cls = com.davidflanag an.Class;
cls.foo = function(){aler t('bar');};
com.davidflanag an.Class.foo(); // shows "bar" but maybe should be
"foo"

with Flanagan's importing system

var com = {davidflanagan: {Class:{}}};
com.davidflanag an.Class.foo = function() {alert('foo');} ;
var cls = {};
cls.foo = com.davidflanag an.Class.foo; // import one symbol
cls.foo = function(){aler t('bar')}; // subsequently change it in the
new namespace
com.davidflanag an.Class.foo(); // shows "foo", so original namespace
not affected.

I'm just guessing on his intent.

Peter

Jan 28 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2278
by: CB | last post by:
I have a group of documents that previously had no schema, now I have a schema, and I want to use XSLT to fixup the documents by inserting the namespace reference. In other words, my input document looks like <SiteAdaptationFile> ... </SiteAdaptationFile>
4
6114
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ---------------------------------------------------------------------------------------------------------------------------------------------------------- The @ Import Directive Next to @ Page, the directive that ASP.NET programmers use the most is @ Import. The @ Import directive is ASP.NET's...
6
3412
by: ryan.d.rembaum | last post by:
Hello, I have code that I wish to use in many web applications. Basically sort of stand utility stuff. So from Visual Studio Project I select add a component and chose Component Class. Lets say I enter code at the end of this question in to the code section. How then would I reference this in a new Web Application (or in the same web application for that matter?)
47
3376
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
4
3088
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which leads to a secondary problem involving php's __autoload feature. Since you cannot specify a namespace when calling a class that may not have been included, you are forced to store all of your classes in the same folder in your file system. This...
2
1595
by: ºa¤Ö | last post by:
I have two class which wroten in C#, they are Company and Address, and I want define they are under a same namespace, I found a lot but not much resource talking about it, i tried following method // Company.cs using System; namespace Project1.Domain.Company { public class Company { public Company() {}
1
1475
by: Alok Sathaye | last post by:
Hi, I wanted to use the <msxsl:script> tag in a XSLT and include a .Net defined namespace. For ex: System.Web It seems the framework 1.1 does not work in a manner mentioned in the documentation. The framework gives me a run-time exception and does not transform the input file Have you come across such situations and were able to implement the same?
6
1524
by: jdemoor | last post by:
Hi, I'm new to Python and have the following problem : I have an application started by a main.py file, which does a ' from module_1 import * '. main.py is responsible from the creation of an object which is then used in module_1. What is the best way to make that object visible in the module_1 namespace ? I guess that if I do an ' import module_1 ', I can make the object
5
2191
by: mark_galeck_spam_magnet | last post by:
Hi, why does complain name 'compileFile' not defined. But works. Why? (I did read the tutorial, it seems to say "import module" should work. Thank you, Mark
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.