Accessing Properties in JXA Libraries?

Hi,

I can call the function in a library, but I can't access a property.
Here's my test library called add.

x = 1
function f(n) {
    return x+n
}

Then I access like this.

lib = Library("add")
console.log(lib.x) // prints function () { [native code] }
console.log(lib.f(3)) // prints 4

How can I access property x in the add library?
Thanks!

The Automation.Library() mechanism is very fragile and limited – methods only, and they must be encoded with the function (rather than arrow) syntax.

If you wanted to store a constant value, you would have to encode and fetch it as a function, e.g.

Library

function test (message) {
   const arrowed = `-> ${message} <-`;

   return (
       console.log(arrowed),
       arrowed
   );
}

function x() {
    return 1;
}

Call

(() => {
    'use strict';

    const toolbox = Library('toolbox');
    
    // return toolbox.test('some argument or other');
    
    
    return toolbox.x();
})();

For updatable values that persist between sessions, you would need to use a KM variable.

Hmm, that's unfortunate..
I was also hoping to be able to return an object and access like below, but no luck.

function object() {
    var o = {}
    o.x = 1
    o.f = function (n) {
        return this.x+n
    }
    return o
}

Then call like this.

lib = library("add")
o = lib.object()
console.log(o.x) // prints 1
console.log(o.f(3)) // error: o.f is not a function.