Howto:Understand Namespaces and Methods: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
Line 16: Line 16:
   house3.bath
   house3.bath


Here, houseX is the enclosing namespace/context, i.e. "bath" is a member or "field" of the namespace.


Basically, namespaces make it possible to have identically named variables/symbols without them possibly "clashing" or "polluting" the global namespace.
Basically, namespaces make it possible to have identically named variables/symbols without them possibly "clashing" or "polluting" the global namespace.


Before namespaces were used, there were only "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable):
Otherwise, you would have to use different symbols with explicit naming conventions, such as:
 
house1_bath
house2_bath
house3_bath
 
These naming conventions can become awkward pretty quickly.
 
Before namespaces were used, there were only such "global variables", so whenever some piece of code referred to a variable, it was possible to clash with some other similar or even unrelated uses of the variable (imagine a counter variable):


That's when people started providing a surrounding "context" to embed variables properly.
That's when people started providing a surrounding "context" to embed variables properly.
Line 30: Line 39:
     }
     }


Here, x is declared to be specific to the "hello" function and its namespace.




Here, x is declared to be specific to the "hello" function and its namespace.
In Nasal, a namespace is just a conventional hash:
 
var foo = {};
 
 
To add members or fields to this "context" (or namespace), you can use several different notations:
 
foo.altitude = 100;
foo["altitude"] = 100;
 
or even specify fields during initialization:
 
var foo = { altitude:100 };
 
In order to access these fields or "members" of a namespace, you need to provide the valid namespace first:
 
print ( foo.altitude );


So, basically namespaces are all about organizing and structuring your variables and the overall symbol space.
So, basically namespaces are all about organizing and structuring your variables and the overall symbol space.
Line 53: Line 79:




Obviously, this will only work if the switch off routine (method!) has some house to work with. The class itself really is just a "template" for functionality, before it can be used it needs to be instantiated, i.e. a new object must be created using the template, and then the member functions (methods) can be called.
Obviously, this will only work if the switch off routine (method!) has some house to work with. The class itself really is just a "template" for functionality, before it can be used it needs to be instantiated, i.e. a new object (house) must be created using the template, and then the member functions (methods) can be called for that object.




Navigation menu