1

Whenever I try to extend the Object prototype, I get an error:

Error #1056: Cannot create property my_extension on mx.core.UIComponentDescriptor.

I searched around, and found these:

Flash AS3: ReferenceError: Error #1056: Cannot create property

ReferenceError: Error #1056 - instance name Error

I'm not using a visual IDE for designing a stage, but MXML and AS3 files, so I'm not sure what to make of this error.

My code:

Object.prototype.keys = function(): Array {
  var keys: Array = [];

  for (var key: * in this) {
    keys.push(key);
  }

  return keys;
}

Object.prototype.values = function(): Array {
  var values: Array = [];

  for each (var value: * in this) {
    values.push(value);
  }

  return values;
}
1
  • put code in bracers : object["keys"](); , because if You write without on nondynamic object , You will get compile-time error. Anyway , I would advice You for example: create class ObjectUtil and inside function getKeys , than use ObjectUtil.getKeys(myObject); Commented Jun 22, 2012 at 6:42

1 Answer 1

4

Using prototype to extend a class seems very ActionScript 1 or 2.

In AS3, you may be able to prototype if the class is dynamic.

There are downsides to prototype:

  • Only dynamic classes can be extended, one can not add methods to Math for example.
  • Calls to methods stored in the prototype take longer to execute.
  • Since methods are added at run-time, editors can not show them with code hinting or use the correct syntax highlighting.

Since all classes extend object, it is not necessary to explicitly declare Object as a base; however, you could define an AbstractObject class to be extended:

package
{

    public dynamic class AbstractObject extends Object
    {

        public function AbstractObject()
        {
            super();
        }

        public function get keys():Array
        {
            var keys:Array = [];

            for (var key:* in this)
            {
                keys.push(key);
            }

            return keys;
        }

        public function get values():Array
        {
            var values:Array = [];

            for each (var value:* in this)
            {
                values.push(value);
            }

            return values;
        }

    }
}

Subtype AbstractObject for your classes.

Sign up to request clarification or add additional context in comments.

8 Comments

Do you need extends Object? I thought every class was an object by default?
No, which I stated in my answer.
Extending Object won't work for my purposes. I still don't really understand - are you implying Object is a static class?
Instead of prototyping members on to class definitions, use object oriented paradigms of development by extending a base class. Otherwise, use some static ObjectUtil class as recommended for this functionality.
I +1 this great answer and plus the guy asking the question will probably not accept any answers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.