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;
}
Community
  • 1
  • 1
Kevin Li
  • 1,540
  • 2
  • 17
  • 23
  • 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); – turbosqel Jun 22 '12 at 06:42

1 Answers1

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.

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
  • Do you need extends Object? I thought every class was an object by default? – Neil Jun 22 '12 at 13:28
  • No, which I stated in my answer. – Jason Sturges Jun 22 '12 at 14:09
  • Extending Object won't work for my purposes. I still don't really understand - are you implying Object is a static class? – Kevin Li Jun 22 '12 at 19:16
  • 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. – Jason Sturges Jun 22 '12 at 21:05
  • I +1 this great answer and plus the guy asking the question will probably not accept any answers. – The_asMan Jun 23 '12 at 16:05
  • Although I've seen plenty of people mention it, I have never seen any benchmarks that prove there is a impact on performance when adding methods to a native type's prototype. I know that Flash looks for Class-level methods first, and then prototype methods if none are found in the class, but in the case of native types, isn't every method on the prototype? For reference, I found this short article about extending "core types" http://tobyho.com/2009/05/02/modifying-core-types-in/ – Steve Mason May 06 '16 at 19:53
  • I would like to mention that what brought me here is that I am rewriting a bunch of Array methods that have been introduced recently in JavaScript. I (unfortunately) am required to use ActionScript because I work in adtech, and I'm trying to use as much of the recent advancements in ecmascript as I can to improve my code. Aside from mutating the prototype or extending native types, you may also use `call()` on your method. For example, `ArrayUtils.reduceRight.call([1,2,3], myReducerFn)`. – Steve Mason May 06 '16 at 19:59
  • @SteveMason Internally within the AVM2, traits object is optimized with details of a class, reducing memory and reducing direct machine instructions. Prototype chains are legacy, providing an alternative inheritance mechanism, and incur overhead in strict mode requiring dynamic type checking. – Jason Sturges May 07 '16 at 02:08