NSB Cosmic Center Image

N.S.B. Cosmic Center

NSB Cosmic Center Image

JavaScript & MathScript Interactive Document Generator-Demo-05

This document is generated by N.S.B. JavaScript & MathScript Interactive Document Generator version 001.

Introduction to N.S.B. JavaScript & MathScript Interactive Document Generator

JavaScript Objects

The easiest way to define an object in JavaScript is to use the literal notation. For instance, this defines an object that describes three coordinates:

input(1)=> p={ x:0, y:0, z:0 }
output(1)=> [object Object]

Notice that the three properties of the above-defined object are separated by commas; the name of each property is separated from its initial value by a semicolon.

The following code sequence illustrates some basic JavaScript methods for dealing with an object:

input(2)=> typeof(p)
output(2)=> object
input(3)=> p.hasOwnProperty('x') 
output(3)=> true
input(4)=> p.x 
output(4)=> 0
input(5)=> p.x=2.3; p.y=-4.5; p.z=11 
output(5)=> 11
input(6)=> [p.x, p.y, p.z] 
output(6)=> [ 2.300, -4.500, 11 ]

In the above, we have demonstrated how to check the existence of a property in a defined object using the method function hasOwnProperty(); how to get and how to set the value of a property. The above array gives the new values.

It is most interesting with regard to dealing with JavaScript objects that you can define a property as a reference to a similar (or different) object. This enables one to create a chain of linked objects. The following code sequence illustrates this point:

input(7)=> p={ x:1.0, y:2.0, z:3.0, next:null }; q={ x:1.1, y:2.2, z:3.3, next:null }; r={x:1.2, y:2.3, z:3.4, next:null } 
output(7)=> [object Object]
input(8)=> p.next=q 
output(8)=> [object Object]
input(9)=> q.next=r; r.next=p  
output(9)=> [object Object]
input(10)=> p.next.x 
output(10)=> 1.100
input(11)=> p.next.next.y 
output(11)=> 2.300
input(12)=> q.next.next.next.z 
output(12)=> 3.300
input(13)=> r.next.x 
output(13)=> 1

More intelligent code can do marvelous things with linked objects. Now since our above-defined objects p, q, r are linked together, let us do the following:

input(14)=>  var aa=[]; var t=p; for(var i=1; i<10; i++){ aa.push([t.x, t.y, t.z]); t=t.next; } aa
output(14)=> [ [ 1, 2, 3 ], [ 1.100, 2.200, 3.300 ], [ 1.200, 2.300, 3.400 ], [ 1, 2, 3 ], [ 1.100, 2.200, 3.300 ], [ 1.200, 2.300, 3.400 ], [ 1, 2, 3 ], [ 1.100, 2.200, 3.300 ], [ 1.200, 2.300, 3.400 ] ]

If you have been following this and the preceding demodocs without difficulty, then your knowledge of JavaScript is adequate, and you are now ready to start following our development of MathScript.

Next
NSB Cosmic Center Image

Home
N.S.B. Cosmic Center

NSB Cosmic Center Image