473,480 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Create 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.registerNamespace 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 3160
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....which 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
2359
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...
4
3199
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...
7
7415
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 = {}; ...
0
6412
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...
21
6019
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...
13
2069
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...
3
1734
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...
8
4005
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...
20
2255
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
6915
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...
0
7054
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,...
0
7097
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...
1
6750
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...
1
4794
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...
0
4493
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...
0
3003
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...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
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...

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.