Search code examples
javascriptjscriptadvanced-installer

How does this left hand side assignment for a function call work in JScript?


I was looking for a way to log down messages the installer log using JScript and stumbled upon this answer:

How to debug an MSI Custom Action that is implemented in Javascript?

It works great. However there is something intriguing in the code that I haven't been able to figure out. In this method:

// spool an informational message into the MSI log, if it is enabled. 
function LogMessage(msg) {
    var record = Session.Installer.CreateRecord(0);
    record.StringData(0) = "CustomAction:: " + msg;
    Session.Message(MsgKind.Log, record);
}

How does the line record.StringData(0) = "CustomAction:: " + msg; work, from a syntax/semantic perspective? It looks to me this is trying to assign a value to the return value of a function call, which should be illegal or a no-op at best? Yet it works, and the message is printed out in the log.

What am I missing?


Solution

  • It is a JScript way to access lists and not an actual function. Therefore it does not throw an Invalid left-hand side in assignment.

    StringData is a list of values. What you are actually setting is the value of the 0 index. It is like setting the value to an array using arr[0] = 'xyz'. In your example you could also omit it:

    record.StringData = "CustomAction:: " + msg;
    

    Syntax

    propVal = Record.StringData
    Record.StringData = propVal 
    

    Property value

    Required field number of the value within the record, 1-based.

    Remarks

    The returned value of a nonexistent field is an empty string. To set a record string field to null, use either an empty variant or an empty string. Attempting to store a value in a nonexistent field causes an error.

    Source: Patrick