1

When I try to use Object.keys function in JavaScript file that runs as Java application I get Exception.

I want to use some function from JS file like this:

    function calculateProductPrice(orderData)
    {
        ...
        k = Object.keys(prices);    
        for (var i = k.length; i > -1; i--)
        {
            ...
        }       
        ...     
    }

    var prices = {
             "1":[
                99,
                106,
                113,
               ...

Fragment of java code that use this script:

Context context = Context.enter();
ScriptableObject  scope = context.initStandardObjects();
FileReader fr = new FileReader("script.js");
context.evaluateReader(scope, fr, "<cmd>", 1, null);
Object orderData = Context.javaToJS(new OrderData(), scope);
scope.put("orderData", scope, orderData);
Object result = context.evaluateString(scope, "calculateProductPrice(orderData)", "<cmd>", 1, null);

And I get Exception:

Exception in thread "main" org.mozilla.javascript.EcmaError: TypeError: Cannot find function keys in object function Object() { [native code for Object.Object, arity=1] }
. (<cmd>#11)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3654)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3632)
    at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3660)
    at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3679)
    at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3743)
    at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2247)
    at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2214)
    at org.mozilla.javascript.gen.c1._c1(<cmd>:11)
    at org.mozilla.javascript.gen.c1.call(<cmd>)
    at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:97)
    at org.mozilla.javascript.gen.c2._c0(<cmd>:1)
    at org.mozilla.javascript.gen.c2.call(<cmd>)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
    at org.mozilla.javascript.gen.c2.call(<cmd>)
    at org.mozilla.javascript.gen.c2.exec(<cmd>)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1104)
    at net.terraincognita.restapi.service.ScriptEngine.<init>(ScriptEngine.java:65)
    at net.terraincognita.restapi.service.ScriptEngine.main(ScriptEngine.java:94)
Java Result: 1
1

2 Answers 2

3

This method is not defined for the Object class in Rhino.

The Rhino documentation states:

Rhino contains

  • All the features of JavaScript 1.7

The Mozilla JavaScript documentation states Object.keys was:

Introduced in JavaScript 1.8.5

The same Object.keys documentation includes an example of how to add this to previous version of JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

3

Object.keys() is an ECMAScript 5 feature introduced in Javascript 1.8.5.

Rhino, on the other hand, is based on Javascript 1.7.

So, unfortunately, you're stuck with:

for (var i in prices) {
  if (prices.hasOwnProperty(i)) {
    // do stuff
  }
}

1 Comment

I cannot see the method hasOwnProperty() either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.