Howto:Understand Namespaces and Methods: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
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.





Revision as of 10:35, 23 December 2011

Namespaces

A name space is a "context", i.e. an environment where a certain symbol is valid.

Imagine, three houses with rooms. Even otherwise identical rooms are kepts in separate "environments", i.e. you have to refer to a certain "environment" (namespace) to uniquely identify a certain room.

For example, imagine three bath rooms in these three houses: house1, house2, house3.

Now, imagine the users of the houses want to refer to each bath room using just the word (symbol/variable) "bath". To be able to resolve this properly, they will also need to qualify what house they are referring to, i.e. if it's house1, house2, house3.


  house1.bath
  house2.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.

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. In Nasal space, variables declared in functions will by default by specific to the function's scope and not be directly accessible:


   var hello = func {
     var x =100;
   }

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.

Just imagine it like an "area code", where people living in different countries, states, districts, counties, towns get a DIFFERENT prefix code, even though they may have the same telephone number otherwise. The international prefix number (i.e. 001 for the US) makes it obvious that the following number is a US number, same goes for local area codes (752, 642, 543).


Click here for more information.


Methods

Methods are somewhat related to "namespaces" in that they are class-specific functions (OOP), i.e. functions that are specific to a certain instance of an already instantiated class. In Nasal space, this means that the function is embedded inside a Nasal hash and that it makes use of instance data (using "me") or accessing the parents vector.

For example, to switch off the lights in the bath room, there could be a method "switch_off_lights" in the "house" class:


  house.bath.switch_off_lights


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.


Click here for more information.


More Information

http://en.wikipedia.org/wiki/Namespace

http://en.wikipedia.org/wiki/Method_%28computer_programming%29

http://en.wikipedia.org/wiki/Object-oriented_programming