473,835 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript Namespace

Frinavale
9,735 Recognized Expert Moderator Expert
So, today I learned how to define classes, create instances of Objects, and use inheritance. I thought I was all set to go and was excited about finally getting something accomplished today (other than just learning stuff).

Well I didn't get far.

I'm stuck on Namespaces in JavaScript.

The following does not work:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. Type.registerNamespace("MyNamespace");
  6.  
  7. MyNamespace.Foo = function(firstNumber,secondNumber){
  8.   this.a = firstNumber; //"a" is now a property of the Foo Object
  9.   this.b = secondNumber;//"b" is now a property of the Foo Object
  10. };
  11. MyNamespace.Foo.prototype = {
  12.     Add: function(){return this.a + this.b;},
  13.     Divide: function(){return this.a/this.b;},
  14.     Multiply: function(){return this.a * this.b;}
  15. };
  16. MyNamespace.Foo.registerClass('MyNamespace.Foo');
  17.  
  18. var MyNamespace.instanceOfFoo = new Foo(2,3);
  19. document.write(instanceOfFoo.Multiply());
  20. document.write("<br />");
  21.  
  22.  </script>
  23.  
  24. </body>
  25. </html>
From what I understand, the Type.registerNa mespace method to creates a namespace where I can place my classes required in my project.

If I remove the Namespace, it works fine:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5.  
  6. Foo = function(firstNumber,secondNumber){
  7.   this.a = firstNumber; //"a" is now a property of the Foo Object
  8.   this.b = secondNumber;//"b" is now a property of the Foo Object
  9. };
  10. Foo.prototype = {
  11.     Add: function(){return this.a + this.b;},
  12.     Divide: function(){return this.a/this.b;},
  13.     Multiply: function(){return this.a * this.b;}
  14. };
  15.  
  16. var instanceOfFoo = new Foo(2,3);
  17. document.write(instanceOfFoo.Multiply());
  18. document.write("<br />");
  19.  
  20.  
  21. </script>
  22.  
  23. </body>
  24. </html>
Could someone please explain what is going on here?
Is the Type class part of the Ajax Framework that ASP.NET uses? Or is it a native JavaScript Object?

Thanks again,

-Frinny
Feb 20 '09 #1
1 3192
Frinavale
9,735 Recognized Expert Moderator Expert
Oh, never mind.

The Type class is part of the Microsoft AJAX Library and this is why my attempts at using the Type class in the w3c "Tryit Editor" isn't working.

I was attempting to keep things as simple as possible...but Microsoft decided to complicate them further by providing an "easy to use" framework....wh ich is proving very difficult to understand.
Feb 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2384
by: Dan Webb | last post by:
Hi All, Im currently working on ways of pacakaging javascript functions/variables/objects in a similar way to the Java package statement so that scripts can be interact with each other with the minimum risk of clashing (due to function/variable name clashes). Do any of you do this already and/or are there any techniques you've come across? Im aware that ECMAScript 2 has this facility and have seen several
4
3231
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
7
7442
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; com.unFocus.Namespaces = new function() { this.register = function(namespace) { namespace = namespace.split('.');
0
6444
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to be modified: if(e.CommandName =="Print") { string parsedreceipt = null; parsedreceipt = DecodeReceipt (e.Item.Cells.Text); Session = parsedreceipt;
21
6061
by: petermichaux | last post by:
Hi, I've been asking questions about library design over the last week and would like to get feedback on my overall idea for a JavaScript GUI library. I need a nice GUI library so there is a good chance I will write this as I need new widgets. I haven't found anything like this and I'm surprised/disapointed this doesn't already exist. My library prototype works nicely. I think parts of these ideas are not commonly used for JavaScript...
13
2142
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different parts of the code, but as far as I can see, the way objects work in javascript is quite awkward. I had it working the way they suggest in the book, and it was going OK until I wanted to call one object method from another - I couldn't find a...
3
1757
by: petermichaux | last post by:
>From Flanagan's JavaScript: The Definitive Guide 5th edition. 1.1.2 JavaScript is Not Simple Programmers who attempt to use JavaScript for nontrivial tasks often find the process frustrating if they do not have a solid understanding of the language. 1.2 Versions of JavaScript
8
4037
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form has a Button. When the Button is clicked, I want a JavaScript confirm dialog to pop-up with the options 'OK' & 'Cancel'. I have done this using the following code: Sub Page_Load(....) btnClick.Attributes.Add("OnClick", "javascript:return...
20
2308
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
0
9802
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
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10807
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...
0
10517
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9343
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...
1
7765
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6961
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();...
2
3990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3086
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.