Objects as Associative Arrays

Objects as Associative Arrays

As you may know, the dot (.) operator can be used to access the [] operator used with arrays.
 <script language="javascript" type="text/javascript">  
 <!--  
 // These are the same  
  object.property  
  object["property"]  
 //-->  
 </script>  
The important thing to notice is that in the object syntax the property is an identifier, whereas in the array syntax, it's a string. The obvious benefits of using an array syntax to access an object is because of the literal data type, you can easily concat strings and play around with them to access an object. For this to work with the standard syntax, an eval() would need to be used.

How do I loop through properties in an object?

You need to use a for/in loop.
 <script language="javascript" type="text/javascript">  
 <!--  
 testObj = {  
      prop1:"hello",  
      prop2:"hello2",  
      prop3:new Array("helloa",1,2)  
 }  
 for(x in testObj) alert( x + "-" + testObj[ x ] )  
 //-->  
 </script>  

What about JScript.NET?

Once you have mastered JScript, you will find it easy to move over to .NET
As the .NET compiler will compile your code into the IL (Intermediate Language) you have the same power to create fully fledged applications as a C# programmer. The great thing about JScript.NET is that it supports typed and untyped programming, (for simple things) so you can still churn out apps quickly.
If you are making complicated Interfaces and such, there is no automatic recognition for this.
Getting as quickly to OOP as possible, I will quickly run over the slightly new syntax with you.
 <script language="JScript" runat="server">  
 var tim : String = "hello";  
 foo : Array = ["1", "2", "3"];  
 </script>  
 You may now explicitly set the data type for a variable.  
 To make an object, you can use the old fashioned way with JScript, or make a class. For example;  
 <script language="JScript" runat="server">  
 public class timObject (n) {  
      private tim : String = hello; // tim is a property of the object  
      private name : String = n;  
      private talk : Function = function(e) { // method  
           alert ( this.name + " says hello")  
      }  
 }  
 </script>  
Is there a prototype equivalent?
Yes; but you may still use prototype inheritance.
 <script language="JScript" runat="server">  
 public class extends timObject {  
      function changeName( n ) {  
           this.name = n;  
      }  
 }  
 </script>  

It is possible in .NET to make entire web services simply by making a class that extends the framework.

reference : http://www.javascriptkit.com/javatutors/oopjs4.shtml

Comments

Post a Comment

silahkan berkomentar, kritik dan saran yang membangun adalah harapkan kita semua !

Popular posts from this blog

flexigrid + php with add and edit button

Data Flow Diagram (DFD)

VERIFIKASI SUARA MENGGUNAKAN METODE MFCC DAN DTW MENGGUNAKAN METODE MFCC DAN DTW (BAB II TINJAUAN PUSTAKA)