Avenger
Avenger

Reputation: 365

Classic ASP: Discern VBScript arrays in JScript code

I have a function in JScript that accepts a string or an array as parameter:

<script language="JScript" runat="server">
  function handleThis(what) {
    if (typeof what === 'string') {
      response.write("You provided: " + what + "\n");
    } else if (typeof what === 'object' && what.constructor === Array) { // JS array (ECMAScript3 has no Array.prototype.isArray())
      response.write("You provided: " + what.join(", ") + "\n");
    } else {
      response.write("The provided value is neither a string or an array.\n");
    }
  }
</script>

But then it always falls in "The provided value is neither a string or an array", and typeof what becomes unknown.

I tried checking for its type, and calling new Enumerator() similarly to the guide on traversing collections using JScript, from Microsoft, only to get Object not a collection:

var what_parsed;
try {
  what_parsed = new Enumerator(what);
} catch (e) {
  response.write("Unable to enumerate provided value: " + e.message);
  what_parsed = ["nothing I could understand"];
}

response.write("After a lot of effort, you provided: " + what_parsed.join(", ") + "\n");

Is there a way I could check that unkonwn type object and effectively parse or convert into the JScript scope, in a transparent manner for the user of that function? I'd like to avoid having to convert (from VBScript) that array every time I want to pass a call for the JScript handlers.

For instance, if I call this from JScript, I get:

handleThis("one"); // get "You provided: one"
handleThis(["one", "two"]); // get "You provided: one, two"

But then from VBScript:

handleThis "one" ' get "You provided: one"
handleThis Array("one", "two") ' get "The provided value is neither a string or an array."

And if I add the try-catch block, the last command results in:

handleThis Array("one", "two")
 'Unable to enumerate provided value: Object not a collection
 'After a lot of effort, you provided: nothing I could understand

Upvotes: 2

Views: 20

Answers (0)

Related Questions