Object.hasOwnProperty( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Object.hasOwnProperty( ) Method Flash 6 (undocumented)

checks whether a property is defined directly on an object
someObject.hasOwnProperty(propName)

Arguments

propName

A string indicating the name of the property to check.

Returns

A Boolean; true if propName is a direct property or method of someObject, otherwise false.

Description

The undocumented hasOwnProperty( ) method returns a Boolean indicating whether someObject directly defines a property or method with the name propName, in which case it is true; otherwise, it is false. It is used mostly during debugging to distinguish a property assigned directly to an object instance from a property inherited from a class via the prototype chain. For example:

// A Rectangle class function Rectangle (w, h) {   // Direct properties   this.width = w;   this.height = h; } // An inherited property Rectangle.prototype.numSides = 4;     // A Rectangle instance rect = new Rectangle(10, 100); // Check direct property: trace(rect.width);                       // Displays: 10 trace(rect.hasOwnProperty("width"));     // Displays: true     // Check inherited property: trace(rect.numSides);                    // Displays: 4 trace(rect.hasOwnProperty("numSides"));  // Displays: false

Notice that numSides is accessible through rect, but is not defined on rect itself, so rect.hasOwnProperty("numSides") returns false. However, it would also return false if the property wasn't defined anywhere (on either rect or its class).

Reader Exercise: Write a custom function, hasProperty( ), that returns the number 2 if hasOwnProperty( ) is true, returns 1 if hasOwnProperty( ) is false but the property is inherited from a class, and returns 0 if the property doesn't exist for the object. (Hint: Even a declared property can contain an undefined value, so use hasOwnProperty( ) to check each object in the prototype chain. If you get to the Object class and hasOwnProperty( ) still returns false, the property doesn't exist.)

Reader Exercise 2: Write a custom function, showProperties( ), that displays the names and values of all of an object's properties and indicates which are read through the prototype chain.

Reader Exercise 3: Write a custom function, showMethods( ), that is analogous to showProperties( ) but displays the names of an object's methods instead of its properties. (Hint: use instanceof to determine if a property is an instance of the Function class).

The hasOwnProperty( ) method can be used with getter/setter properties added via Object.addProperty( ). It is defined by the ECMA-262 specification but not officially documented by Macromedia.

See Also

instanceof, Object._ _proto_ _; Chapter 12



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net