when writing a restlet function, does anyone know ...
# suitescript
r
when writing a restlet function, does anyone know the parent object I can refer to to run dynamic function names from within the return? Example:
Copy code
return {
    post: function (context) {
        switch (context.action) {
            case 'doStuff': return doStuff(context);
        }
    }
};
I'd like to clean up to
Copy code
return {
    post: function(context) {
        return <parentObject>[context.action](context);
    }
};
b
like this?
Copy code
var parentObject = {doStuff: function(){}}
return {
    post: function(context) {
        return parentObject[context.action](context);
    }
};
r
that's my last resort, is wrapping everything within the definer function within it's own object, but I was trying to avoid that
j
Maybe I don't entirely understand what you are looking for, but there's no
<parentObject>
mapping names to functions until you make one.
r
operating within the definer callback, I was hoping there was a undocumented reference to
this
that I could call from within it
where
this
would be the
<parentObject>
j
You could move your action functions to another script file. Then when you import the other file in the define statement all the functions will be named properties of the corresponding imported object.
Other than that, afaik there's no magic "closure" variable like python's
locals()
.
r
yea.... was kind've hoping for something similar within NetSuite's Rhino
j
Don't think there is 🙁
b
im not sure why you want to avoid closures
but an alternative is
Copy code
return {
    doStuff: function(){},
    post: function(context) {
        return this[context.action](context);
    }
};
r
I actually tried that, but NetSuite replied that the function did not exist within the object, which makes me thing
this
in that context isn't referencing the closure defined as part of
define()
@battk I don't want to avoid closures, just avoid writing a switch statement for every possible branch
j
I think your two options are 1) explicitly define an object that maps action names to functions 2) move your functions to another script file and have the import statement automatically create the object in step 1
and then 3) a switch statement
r
thanks, combined with @erictgrubaugh comment, I have a direction to go in