4 optional object properties – Casio Naurtech CETerm Ver.5.5 Scripting Guide User Manual
Page 24

N
AURTECH
W
EB
B
ROWSER AND
T
ERMINAL
E
MULATION FOR
W
INDOWS
CE
AND
W
INDOWS
M
OBILE
CETerm Scripting Guide
Page 24
2.8.4 Optional Object Properties
The object literal returned as a result may not always contain all possible
properties. This is true for the File.GetList() method. The various file timestamps
are not always present. Your JavaScript code must check these values or be
prepared to handle th
e “undefined” value that can result.
When first developing your application, it can be helpful to display the literal
within a message box using OS.Alert() in order to review the contents that are
returned in your environment. You can then tailor your JavaScript to process the
contents.
var getListResult = OS.File.GetList( "/System/*" );
OS.Alert("GetList results:\n" + getListResult );
Following is a sample of one technique to test for a property before using the
property. This technique uses the JavaScript Object.hasOwnProperty() method
to check for existence. The File.GetList() method may return a result which is
missing the lastAccessTime for directory entries
// Sample GetList literal result
[ {name:"myApp.cab", attributes:0x21,
creationTime:new Date(2006,11,15,11,51,41,480),
lastAccessTime:new Date(2007,7,27,3,27,41,0),
lastWriteTime:new Date(2008,6,15,0,29,50,0), size:2455494},
{name:"myconfig.ini", attributes:0x21,
lastAccessTime:new Date(2007,7,27,3,27,18,0),
lastWriteTime:new Date(2008,6,15,0,29,48,0), size:12564},
{name:"AppDirectory", attributes:0x10,
lastWriteTime:new Date(2008,2,11,12,29,49,0), size:1024} ]
var getListResult = OS.File.GetList( "/System/*" );
eval("var fileArray=" + getListResult );
var i;
var file;
var time;
for (i=0; i
file = fileArray[i];
if (file.hasOwnProperty( "lastAccessTime" ))
{
time = file.lastAccessTime;
OS.Alert( "Last Access in " + time.getFullYear() );
}
}