This is the draft for the Object class.  It's very similar to the Object
object in ES3, the only addition (to my knowledge) is the extra
parameter to propertyIsEnumerable.  And of course the specification
formalism is new.

Please comment.

--lars
Title: The class "Object"

The class Object


FILE:                       spec/library/Object.html
DRAFT STATUS:               DRAFT 1 - 2008-03-05
REVIEWED AGAINST ES3:       YES
REVIEWED AGAINST ERRATA:    YES
REVIEWED AGAINST BASE DOC:  YES
REVIEWED AGAINST PROPOSALS: YES
REVIEWED AGAINST CODE:      YES

The class Object is a dynamic non-final class that does not subclass any other objects: it is the root of the class hierarchy.

All values in ECMAScript except undefined and null are instances of the class Object or one of its subclasses.

NOTE   Host objects may not be instances of Object or its subclasses, but must to some extent behave as if they are (see Host objects).

Synopsis

The class Object provides this interface:

public dynamic class Object
{
    public function Object(value=undefined) …
    static meta function invoke(value=undefined) …

    static public const length = 1

    intrinsic function toString() : string …
    intrinsic function toLocaleString() : string …
    intrinsic function valueOf() : Object …
    intrinsic function hasOwnProperty(name: EnumerableId): boolean …
    intrinsic function isPrototypeOf(obj): boolean …
    intrinsic function propertyIsEnumerable(name: EnumerableId, flag: (boolean|undefined) = undefined): boolean …
}

The Object prototype object provides these direct properties:

    toString:             function () … ,
    toLocaleString:       function () … ,
    valueOf:              function () … ,
    hasOwnProperty:       function (V) … ,
    isPrototypeOf:        function (V) … ,
    propertyIsEnumerable: function (name, flag=undefined) … ,

The Object prototype object is itself an instance of the class Object, with the exception that the value of its [[Prototype]] property is null.

Methods on the Object class object

new Object ( value=… )

Description

When the Object constructor is called with an argument value (defaulting to undefined) as part of a new _expression_, it transforms the value to an object in a way that depends on the type of value.

Returns

The Object constructor returns an object (an instance of Object or one of its subclasses, or a host object).

NOTE   The Object constructor is the only constructor function defined on a class in the language whose result may be a value of a different class than the one in which the constructor is defined.

Implementation

The Object constructor can't be expressed as a regular ECMAScript constructor. Instead it is presented below as a helper function makeObject that the ECMAScript implementation will invoke when it evaluates new Object.

The helper function makeObject only is invoked on native ECMAScript values. If new Object is evaluated on a host object, then actions are taken and a result is returned in an implementation dependent manner that may depend on the host object.

helper function makeObject(value=undefined) {
    switch type (value) {
    case (s: string) {
        return new String(s);
    }
    case (b: boolean) { 
        return new Boolean(b);
    }
    case (n: (int|uint|double|decimal)) { 
        return new Number(n);
    }
    case (x: (null|undefined)) { 
        return magic::createObject();
    }
    case (o: Object) {
        return o;
    }
    }
}

Object ( value=… )

Description

When the Object class object is called as a function with zero or one arguments it performs a type conversion.

Returns

It returns the converted value.

Implementation

static meta function invoke(value=undefined)
    new Object(value);

Methods on Object instances

intrinsic::toString ( )

Description

The intrinsic toString method converts the this object to a string.

Returns

The intrinsic toString method returns the concatenation of "[", "object", the class name of the object, and "]".

Implementation

intrinsic function toString() : string
    "[object " + magic::getClassName(this) + "]";

The function magic::getClassName extracts the class name from the object. See magic:getClassName.

intrinsic::toLocaleString ( )

Description

The intrinsic toLocaleString method calls the public toString method on the this object.

NOTE   This method is provided to give all objects a generic toLocaleString interface, even though not all may use it. Currently, Array, Number, and Date provide their own locale-sensitive toLocaleString methods.

NOTE   The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.

Returns

The intrinsic toLocaleString method returns a string.

Implementation

intrinsic function toLocaleString() : string
    this.toString();

intrinsic::valueOf ( )

Description

The intrinsic valueOf method returns its this value.

If the object is the result of calling the Object constructor with a host object (Host objects), it is implementation-defined whether valueOf returns its this value or another value such as the host object originally passed to the constructor.

Returns

The intrinsic valueOf method returns an object value.

Implementation

intrinsic function valueOf() : Object
    this;

intrinsic::hasOwnProperty ( name )

Description

The intrinsic hasOwnProperty method determines whether the this object contains a property with a certain name, without considering the prototype chain.

NOTE   Unlike [[HasProperty]] (see HasProperty-defn), this method does not consider objects in the prototype chain.

Returns

The intrinsic hasOwnProperty method returns true if the object contains the property, otherwise it returns false.

Implementation

intrinsic function hasOwnProperty(name: EnumerableId): boolean
    magic::hasOwnProperty(this, name);

The function magic::hasOwnProperty tests whether the object contains the named property on its local property list (the prototype chain is not considered). See magic:hasOwnProperty.

The helper function toEnumerableId returns its argument if it is one of the member types of EnumerableId (int, uint, string, and Name) and otherwise converts the argument to string.

helper function toEnumerableId(x) {
    switch type (x) {
    case (x: EnumerableId) { return x; }
    case (x: *)            { return string(x); }
    }
}

intrinsic::isPrototypeOf ( obj )

Description

The intrinsic isPrototypeOf method determines whether its this object is a prototype object of the argument obj.

Returns

The intrinsic isPrototypeOf method returns true if the this object is on the prototype chain of obj, otherwise it returns false.

Implementation

intrinsic function isPrototypeOf(obj): boolean
    helper::isPrototypeOf(this, obj);

helper function isPrototypeOf(self, obj): boolean {
    if (!(obj is Object))
        return false;

    while (true) {
        obj = magic::getPrototype(obj);
        if (obj === null || obj === undefined)
            return false;
        if (obj === self)
            return true;
    }
}

The function magic::getPrototype extracts the [[Prototype]] property from the object. See magic:getPrototype.

intrinsic::propertyIsEnumerable ( name, flag=… )

Description

The intrinsic propertyIsEnumerable method retrieves, and optionally sets, the enumerability flag for a property with a certain name on the this object, without considering the prototype chain.

COMPATIBILITY NOTE   The functionality to set the enumerability flag is new in the 4th Edition of this Standard.

NOTE   This method does not consider objects in the prototype chain.

Returns

The intrinsic propertyIsEnumerable method returns false if the property does not exist on the this object; otherwise it returns the value of the enumerability flag for the property before any change was made.

Implementation

intrinsic function propertyIsEnumerable(name: EnumerableId, flag: (boolean|undefined) = undefined): boolean
    helper::propertyIsEnumerable(this, name, flag);

helper function propertyIsEnumerable(self, name, flag) {
    if (!magic::hasOwnProperty(self, name))
        return false;

    let oldval = !magic::getPropertyIsDontEnum(self, name);
    if (!magic::getPropertyIsDontDelete(self, name))
        if (flag !== undefined) {
            print("setting: " + flag);
            magic::setPropertyIsDontEnum(self, name, !flag);
        }
    return oldval;
}

The function magic::hasOwnProperty tests whether the object contains the named property on its local property list. See magic:hasOwnProperty.

The function magic::getPropertyIsDontEnum gets the DontEnum flag of the property. See magic:getPropertyIsDontEnum.

The function magic::getPropertyIsDontDelete gets the DontDelete flag of the property. See magic:getPropertyIsDontDelete.

The function magic::setPropertyIsDontEnum sets the DontEnum flag of the property. See magic:setPropertyIsDontEnum.

Methods on the Object prototype object

Description

The methods on the Object prototype object all call the corresponding intrinsic methods of the Object class.

Returns

The prototype methods return what their corresponding intrinsic methods return.

Implementation

prototype function toString()
    this.intrinsic::toString();

prototype function toLocaleString()
    this.intrinsic::toLocaleString();

prototype function valueOf()
    this.intrinsic::valueOf();

prototype function hasOwnProperty(name)
    this.intrinsic::hasOwnProperty(helper::toEnumerableId(name));

prototype function isPrototypeOf(obj)
    this.intrinsic::isPrototypeOf(obj);

prototype function propertyIsEnumerable(name, flag=undefined)
    this.intrinsic::propertyIsEnumerable(helper::toEnumerableId(name), flag);
_______________________________________________
Es4-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es4-discuss

Reply via email to