Howto:Understand Namespaces and Methods: Difference between revisions

Jump to navigation Jump to search
m (→‎Resources: added link)
Tags: Mobile edit Mobile web edit
 
Line 9: Line 9:


Now, imagine the users of the houses wanting 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.
Now, imagine the users of the houses wanting 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.
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
house1.bath
house1.bath
Line 14: Line 15:
house3.bath
house3.bath
</syntaxhighlight>
</syntaxhighlight>
Here, houseX is the enclosing namespace/context, i.e. "bath" is a member or "field" of the namespace.
Here, houseX is the enclosing namespace/context, i.e. "bath" is a member or "field" of the namespace.


Line 19: Line 21:


Otherwise, you would have to use different symbols with explicit naming conventions, such as:
Otherwise, you would have to use different symbols with explicit naming conventions, such as:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
house1_bath
house1_bath
Line 28: Line 31:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
      var wp1 = 0;
var wp1 = 0;
      var wp1alt = 0;
var wp1alt = 0;
      var wp1dist = 0;
var wp1dist = 0;
      var wp1angle = 0;
var wp1angle = 0;
      var wp1length = 0;
var wp1length = 0;
      var wp1id = "";
var wp1id = "";
      var wp1brg = 0;
var wp1brg = 0;


      var wp2 = 0;
var wp2 = 0;
      var wp2alt = 0;
var wp2alt = 0;
      var wp2dist = 0;
var wp2dist = 0;
      var wp2angle = 0;
var wp2angle = 0;
      var wp2length = 0;
var wp2length = 0;
      var wp2id = "";
var wp2id = "";
      var wp2brg = 0;
var wp2brg = 0;


      var wp3 = 0;
var wp3 = 0;
      var wp3alt = 0;
var wp3alt = 0;
      var wp3dist = 0;
var wp3dist = 0;
      var wp3angle = 0;
var wp3angle = 0;
      var wp3length = 0;
var wp3length = 0;
      var wp3id = "";
var wp3id = "";
      var wp3brg = 0;
var wp3brg = 0;


      var wp4 = 0;
var wp4 = 0;
      var wp4alt = 0;
var wp4alt = 0;
      var wp4dist = 0;
var wp4dist = 0;
      var wp4angle = 0;
var wp4angle = 0;
      var wp4length = 0;
var wp4length = 0;
      var wp4id = "";
var wp4id = "";
      var wp4brg = 0;
var wp4brg = 0;


      var wp5 = 0;
var wp5 = 0;
      var wp5alt = 0;
var wp5alt = 0;
      var wp5dist = 0;
var wp5dist = 0;
      var wp5angle = 0;
var wp5angle = 0;
      var wp5length = 0;
var wp5length = 0;
      var wp5id = "";
var wp5id = "";
      var wp5brg = 0;
var wp5brg = 0;
</syntaxhighlight>
</syntaxhighlight>


Line 75: Line 78:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
    var hello = func {
var hello = func {
      var x =100; # x is local variable specific to the scope of the currently executing function
    var x = 100; # x is local variable specific to the scope of the currently executing function
    }
}
</syntaxhighlight>
</syntaxhighlight>


Line 83: Line 86:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
    var hello = func {
var hello = func {
      var x =100; # x is local variable specific to the scope of the currently executing function
    var x = 100; # x is local variable specific to the scope of the currently executing function
    }
}
    hello();
hello();
    print(x);
print(x);
</syntaxhighlight>
</syntaxhighlight>


Line 93: Line 96:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = {};
var foo = {};
</syntaxhighlight>
</syntaxhighlight>


Line 99: Line 102:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
foo.altitude = 100;  
foo.altitude = 100;  
foo["altitude"] = 100;
foo["altitude"] = 100;
</syntaxhighlight>
</syntaxhighlight>


Line 106: Line 109:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = { altitude:100 };
var foo = { altitude: 100 };
</syntaxhighlight>
</syntaxhighlight>


Line 112: Line 115:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = { altitude:100, latitude:0, longitude:0 };
var foo = { altitude: 100, latitude: 0, longitude: 0 };
</syntaxhighlight>
</syntaxhighlight>


Line 118: Line 121:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = { altitude:100, latitude:0, longitude:0, };
var foo = { altitude: 100, latitude: 0, longitude: 0, };
</syntaxhighlight>
</syntaxhighlight>


Line 124: Line 127:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = { altitude:, latitude:, longitude:, };
var foo = { altitude:, latitude:, longitude:, };
</syntaxhighlight>
</syntaxhighlight>


Line 130: Line 133:


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
var foo = { altitude:nil, latitude:nil, longitude:nil, };
var foo = { altitude: nil, latitude: nil, longitude: nil, };
</syntaxhighlight>
</syntaxhighlight>


In order to access these fields or "members" of a namespace, you need to provide a valid namespace first:
In order to access these fields or "members" of a namespace, you need to provide a valid namespace first:
<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
print ( foo.altitude );
print(foo.altitude);
</syntaxhighlight>
</syntaxhighlight>
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.
In object oriented programming, this concept is very powerful because you cannot only have hash-specific variables but also hash-specific functions. This makes it possible to create new objects by using a template hash and inheriting fields and behavior (methods).
In object oriented programming, this concept is very powerful because you cannot only have hash-specific variables but also hash-specific functions. This makes it possible to create new objects by using a template hash and inheriting fields and behavior (methods).
1,388

edits

Navigation menu