Arrays as class members

R

Rob Richardson

Greetings!

I am trying to port an application written in Python into JavaScript
so it can be invoked from a web page. The JavaScript will be self-
contained in the page; no Internet communication is involved.

My script includes the following class:

function PlcRegister(theOpcGroup, theTagName, theHandle)
{
var Value = 0;
var OpcItem; // not used for now
var OpcGroup;
var ServerHandle;
var TagName;

this.OpcGroup = theOpcGroup;
this.TagName = theTagName; // not used, but we might as well store
it in case we want it.

this.Read = Read;
this.Write = Write;

alert ("Creating PlcRegister object for tag " + theTagName);

// this.ServerHandle = opclibAddTagToGroup(theOpcGroup, theTagName,
theHandle);
this.ServerHandle = opclibAddTagToGroup(this.OpcGroup, this.TagName,
theHandle);
alert ("Tag " + this.TagName + " has server handle " +
this.ServerHandle);

function Read()
{
if (this.OpcGroup == undefined)
{
alert ("Trying to reading tag " + this.TagName + " before OPC group
is specified.");
}
this.Value = opclibReadTag(this.OpcGroup, this.ServerHandle, 2);
return this.Value;
}
function Write(value)
{
this.Value = value;
opclibWriteTag(this.OpcGroup, this.ServerHandle, value);
}
}

I have a second class that includes the following:

function RecipeDef(MaxTempSegments, MaxAtmosSegments, theBaseId,
theChargeNumber)
{
this.TGasStpts = new Array(); // 2 Registers Temperature Track -
GasStpts and Duration
this.TDurations = new Array();
this.ATypes = new Array(); // 4 Registers Atmosphere Track -
Type, Setpoint, Duration, Sign
this.AGasStpts = new Array();
this.ADurations = new Array();
this.ASigns = new Array();

function BuildRecipe()
{
for (Index = 0; Index < this.TempSegmentCount; Index++)
{
alert ("Temperature segment loop index: " + Index);
// Temperature Track
OpcTag = TagPrefix + "TTSeg[" + Index.toString() + "]Temp";
alert ("Trying to build temp seg tag point " + OpcTag);
tempTag = new PlcRegister(RecipeOpcGroup, OpcTag, nHandle);
alert ("This operation creates a reference, not a copy!");
///////////////////// The following line
fails! /////////////////////////////////////////////////////////////////
this.TGasStpts[Index] = tempTag;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
alert ("Reference set.");
nHandle = nHandle + 1;

OpcTag = TagPrefix + "TTSeg[" + Index.toString() + "]Time";
alert ("Trying to build temp seg tag point " + OpcTag);
this.TDurations[Index] = new PlcRegister(RecipeOpcGroup, OpcTag,
nHandle);
nHandle = nHandle + 1;
}
}
}

When my script tries to reference an element of the TGasStpts array,
it fails with an "Object expected" error. Why is it doing that? I
think, from the little reading that I've done, it shouldn't be
necessary to explicitly create an array element. The act of setting
it equal to a newly created object will create the element. Is that
wrong?

Thank you very much!

RobR
 
R

Rob Richardson

I found the problem. It wasn't related to the array at all. The
problem was the call to BuildScript(). Because I am primarily a C++
developer, I thought I didn't have to qualify the call at all. Since
I'm in the same class, BuildRecipe() would know what the current
object is, right?

Wrong.

I had to add a line reading "this.BuildRecipe = BuildRecipe" to make
the BuildRecipe() method available, and then I had to call it as
"this.BuildRecipe()" instead of just "BuildRecipe()".

RobR
 
R

Rob Richardson

I see I left out that call in my original, trimmed version of the
RecipeDef class, so nobody could have helped me anyway. Here's how
that class should have looked:

function RecipeDef(MaxTempSegments, MaxAtmosSegments, theBaseId,
theChargeNumber)
{
this.TGasStpts = new Array(); // 2 Registers Temperature
Track -
GasStpts and Duration
this.TDurations = new Array();
this.ATypes = new Array(); // 4 Registers Atmosphere
Track -
Type, Setpoint, Duration, Sign
this.AGasStpts = new Array();
this.ADurations = new Array();
this.ASigns = new Array();
// this.BuildRecipe = BuildRecipe; // I didn't have this line
either, and I needed it.
BuildRecipe(); // Changing this to "this.BuildRecipe()" fixes
the problem.

function BuildRecipe()
{
for (Index = 0; Index < this.TempSegmentCount; Index+
+)
{
alert ("Temperature segment loop index: " +
Index);
// Temperature Track
OpcTag = TagPrefix + "TTSeg[" +
Index.toString() + "]Temp";
alert ("Trying to build temp seg tag point " +
OpcTag);
tempTag = new PlcRegister(RecipeOpcGroup,
OpcTag, nHandle);
alert ("This operation creates a reference,
not a copy!");
///////////////////// The following line
fails! /////////////////////////////////////////////////////////////////
this.TGasStpts[Index] = tempTag;
///////////////////////////////////////////////////////////////////////////­///////////////////////////////////////////////
alert ("Reference set.");
nHandle = nHandle + 1;


OpcTag = TagPrefix + "TTSeg[" +
Index.toString() + "]Time";
alert ("Trying to build temp seg tag point " +
OpcTag);
this.TDurations[Index] = new
PlcRegister(RecipeOpcGroup, OpcTag,
nHandle);
nHandle = nHandle + 1;
}
}



}
 
T

Thomas 'PointedEars' Lahn

Rob said:
I see I left out that call in my original, trimmed version of the
RecipeDef class, so nobody could have helped me anyway. Here's how
that class should have looked:

function RecipeDef(MaxTempSegments, MaxAtmosSegments, theBaseId,
theChargeNumber)

In contrast to Python (and several other OOPLs), ECMAScript implementations
(up to Edition 3) are OOPLs that use a prototype-based, not a class-based,
inheritance. The above is not a class, it is the function declaration of a
constructor, to be called in a NewExpression to return a reference to a new
object.

http://en.wikipedia.org/wiki/Prototype-based_programming
http://jibbering.com/faq/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top