public class MyObject extends ScriptableObject { @Override public String getClassName() { return "MyObject"; } // define static properties public static void finishInit(Scriptable scope, FunctionObject ctor, Scriptable proto) { ctor.defineProperty("PROP_ONE", 1, READONLY); ctor.defineProperty("PROP_TWO", 2, READONLY); ctor.defineProperty("PROP_THREE", 3, READONLY); }
public static void finishInit(Scriptable scope, FunctionObject ctor, Scriptable proto) { Context cx = Context.getCurrentContext(); Scriptable myObjectInstance = ctor.construct(cx, scope, new Object[] { /* args */ }); ctor.defineProperty("PROP", myObjectInstance, READONLY); .... }
/** * Define this function as a JavaScript constructor. * <p> * Sets up the "prototype" and "constructor" properties. Also * calls setParent and setPrototype with appropriate values. * Then adds the function object as a property of the given scope, using * <code>prototype.getClassName()</code> * as the name of the property. * * @param scope the scope in which to define the constructor (typically * the global object) * @param prototype the prototype object * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#setParentScope * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#setPrototype * @see net.sourceforge.htmlunit.corejs.javascript.Scriptable#getClassName */ public void addAsConstructor(Scriptable scope, Scriptable prototype) { initAsConstructor(scope, prototype); defineProperty(scope, prototype.getClassName(), this, ScriptableObject.DONTENUM); }
void initAsConstructor(Scriptable scope, Scriptable prototype) { ScriptRuntime.setFunctionProtoAndParent(this, scope); setImmunePrototypeProperty(prototype); prototype.setParentScope(this); defineProperty(prototype, "constructor", this, ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY); setParentScope(scope); }