20,741
edits
Line 380: | Line 380: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Next, we are going to add some fields to our new variable "container": | Next, we are going to add some fields to our new variable "container". Each new field can be assigned an initial value, this is done by treating the field as a value/key pair, with a colon (:) separating both. And with multiple fields being separated by a comma (formatting only added for clarity): | ||
<syntaxhighlight lang="php"> | |||
# declare a new hash named waypoint | |||
var waypoint = { | |||
# add a few fields and assign a value to each field: | |||
number:0, | |||
altitude:0, | |||
distance:0, | |||
angle:0, | |||
length:0, | |||
ID:0, | |||
bearing:0 | |||
}; | |||
</syntaxhighlight> | |||
which is equivalent to this, more succinct, version: | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0}; | var waypoint = {number:0,altitude:0,distance:0,angle:0,length:0,ID:0,bearing:0}; | ||
Line 439: | Line 454: | ||
But the really cool thing comes next: | But the really cool thing comes next: | ||
As you may have noticed, the code for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object. This is accomplished using the "parents" keyword: | As you may have noticed, the code (internal structure of fields) for each waypoint is 100% identical - so we could just as well tell the Nasal engine to use an existing hash as a TEMPLATE for a new object instead of having to replicate the hash over and over again. This is accomplished using the "parents" keyword: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
Line 466: | Line 481: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7}; | var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7}; | ||
var new_waypoint = func {return {parents:[waypoint] };} | var new_waypoint = func() { | ||
return { parents:[waypoint] }; | |||
} | |||
var waypoint1 = new_waypoint(); | var waypoint1 = new_waypoint(); | ||
Line 479: | Line 496: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
# this is our template for new waypoints | |||
var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7}; | var waypoint = {number:1,altitude:2,distance:3,angle:4,length:5,ID:6,bearing:7}; | ||
# this is our allocator function creating new waypoints | |||
var new_waypoint = func {return {parents:[waypoint] };} | var new_waypoint = func {return {parents:[waypoint] };} | ||
var waypoints = [nil,nil,nil,nil,nil]; # initialize the vector to set its size | var waypoints = [nil,nil,nil,nil,nil]; # initialize the vector to set its size | ||
# populate the waypoints vector by allocating one new waypoint per "slot" | |||
waypoints[0] = new_waypoint(); | waypoints[0] = new_waypoint(); | ||
waypoints[1] = new_waypoint(); | waypoints[1] = new_waypoint(); | ||
Line 513: | Line 534: | ||
Now, given that the creation of new hashes using a template class is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form: "field_name:value". | Now, given that the creation of new hashes using a template class is such a common thing to do - we could just as well add a new function to the parent hash that we can use to construct new hashes. As you could see already, the fields (or members) of a hash are specified in a well-defined form using the key/value format where key and value are separated by a colon: "field_name:value". | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
Line 538: | Line 559: | ||
ID:6, | ID:6, | ||
bearing:7, | bearing:7, | ||
hello: func { | hello: func { | ||
print("Hello"); | print("Hello"); | ||
Line 595: | Line 617: | ||
var wp = waypoint.new(); | var wp = waypoint.new(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This is what most Nasal code using OOP will typically look like. | |||
== A generic constructor == | == A generic constructor == |