2

In browser there is a handy way of checking what kind of object you are dealing with by writing following code:

Object.prototype.toString.call([]); // "[object Array]"

In xPages SSJS

<xp:text escape="true" id="computedField1">
<xp:this.value>
<![CDATA[#{javascript:Object.prototype.toString.call([]);}]]>
</xp:this.value>
</xp:text>

When I'm doing the same thing in xPages SSJS I'm getting following error message:

Error calling method 'call(Array)' on an object of type 'function [JavaScript Object]'

Why am I getting this error in xPages SSJS and is there a way to get around it?

Naveen
  • 6,786
  • 10
  • 37
  • 85
Murkina
  • 33
  • 3

1 Answers1

4

Every global object or variable in SSJS is an object of a class defined in Java. The statement

Object.prototype.toString.call([]); 

is for JavaScript not for Java. That's why the error. If you wish to find out the class to which the object belongs in SSJS then you can call .getClass().getName().

obj.getClass().getName()

You can find more information here.

Naveen
  • 6,786
  • 10
  • 37
  • 85
  • 2
    A slightly safer syntax is `typeof obj`, which typically returns the same value as `obj.getClass().getName()`, but does not throw an exception if the current value of `obj` is `null` or `undefined`. – Tim Tripcony Feb 05 '14 at 15:46
  • @TimTripcony: Typically, but not all the time. Sometimes *typeof* just returns *objects* while *getCanonicalName* returns the correct class. – Sven Hasselbach Feb 05 '14 at 18:05