public Object construct(Object reference, JSFunction function, Object... args) {
        if (!function.isConstructor()) {
            throw new ThrowException(this, createTypeError("not a constructor"));
        }

        Object ctorName = function.get(this, "name");
        if (ctorName == Types.UNDEFINED) {
            if (reference instanceof Reference) {
                ctorName = ((Reference) reference).getReferencedName();
            } else {
                ctorName = function.getDebugContext();
            }
        }

        // 13.2.2
        // 1. create the new object
        JSObject obj = function.createNewObject(this);
        // 2. set internal methods per 8.12 [DynObject]
        // 3. set class name [DynObject subclass (defaults true)]
        // 4. Set Extensible [DynObject subclass (defaults true)]
        // 5. Get the function's prototype
        // 6. If prototype is an object make that the new object's prototype
        // 7. If prototype is not an object set to the standard builtin object prototype 15.2.4 [AbstractJavascriptFunction]
        // [AbstractJavascriptFunction] handles #7, subclasses may handle #6 if necessary (see BuiltinArray#createNewObject)
        Object p = function.get(this, "prototype");
        if (p != Types.UNDEFINED && p instanceof JSObject) {
            obj.setPrototype((JSObject) p);
        } else {
            JSObject defaultObjectProto = getPrototypeFor("Object");
            obj.setPrototype(defaultObjectProto);
        }

        // 8. Call the function with obj as self
        Object result = call(true, reference, function, obj, args);
        // 9. If result is a JSObject return it

        if (result instanceof JSObject) {
            obj = (JSObject) result;
        }

        // Otherwise return obj
        return obj;
    }
public Object construct ( Object reference , JSFunction function , Object ... args ) { if ( ! function . isConstructor ( ) ) { throw new ThrowException ( this , createTypeError ( "not a constructor" ) ) ; } Object ctorName = function . get ( this , "name" ) ; if ( ctorName == Types . UNDEFINED ) { if ( reference instanceof Reference ) { ctorName = ( ( Reference ) reference ) . getReferencedName ( ) ; } else { ctorName = function . getDebugContext ( ) ; } } JSObject obj = function . createNewObject ( this ) ; Object p = function . get ( this , "prototype" ) ; if ( p != Types . UNDEFINED && p instanceof JSObject ) { obj . setPrototype ( ( JSObject ) p ) ; } else { JSObject defaultObjectProto = getPrototypeFor ( "Object" ) ; obj . setPrototype ( defaultObjectProto ) ; } Object result = call ( true , reference , function , obj , args ) ; if ( result instanceof JSObject ) { obj = ( JSObject ) result ; } return obj ; }
}