代码之家  ›  专栏  ›  技术社区  ›  SSpoke

如何触发阵法推送事件[闪光]

  •  0
  • SSpoke  · 技术社区  · 15 年前

    我试图避免每x毫秒运行一次计时器来检查array对象中的新元素,而不是在检测到新元素时触发事件来处理和删除它们。

    是否可以使用数组?也许是数组集合?两者都可以。

    P.S.>这是 闪光 问题 javascript语言

    4 回复  |  直到 15 年前
        1
  •  1
  •   Obto    15 年前

    为什么不创建自己的array类来扩展array并实现IEventDispatcher,重写push()函数并使其在调用函数时分派事件?

    比如:

    package
    {
        import flash.events.EventDispatcher;
        import flash.events.IEventDispatcher;
    
        public class MyArray extends Array implements IEventDispatcher
        {
            public static var ARRAY_PUSHED:String = "MYARRAY_ARRAY_PUSHED";
    
            private var dispatcher:EventDispatcher;
    
            public function MyArray(...parameters)
            {
                super(parameters);
                dispatcher = new EventDispatcher(this);
            }
    
            override public function push(...parameters):uint
            {
                dispatchEvent(ARRAY_PUSHED);
                super.push(parameters);
            }
    
            public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0, useWeakReference:Boolean=false):void
            {
                dispatcher.addEventListener(type, listener, useCapture, priority);
            }
            public function dispatchEvent(e:Event):Boolean
            {
                return dispatcher.dispatchEvent(e);
            }
            public function hasEventListener(type:String):Boolean
            {
                return dispatcher.hasEventListener(type);
            }
            public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
            {
                dispatcher.removeEventListener(type, listener, useCapture);
            }
            public function willTrigger(type:String):Boolean
            {
                return dispatcher.willTrigger(type);
            }
        }
    }
    
        2
  •  0
  •   Community CDub    8 年前

    Array.prototype.push 方法?被偷自 How to extend Array.prototype.push()?

    Array.prototype.push=(function(){
        var original = Array.prototype.push;
        return function() {
            //Do what you want here.
            return original.apply(this,arguments);
        };
    })();
    

        3
  •  0
  •   SSpoke    15 年前

    建立

     AS3 override function push(...args):uint
        {
            for (var i:* in args)
            {
                if (!(args[i] is dataType))
                {
                    args.splice(i,1);
                }
            }
            return (super.push.apply(this, args));
        }
    

        4
  •  0
  •   PatrickS    15 年前
       private var myArray:Array;
    
       private function addToArray(element:Object):void
       {
            myArray.push(element);
            dispatch( new Event("Element added));
       }
    

    或者在一个静态函数中,如果你想从另一个类调用它

       public static function addToArray( array:Array , element:Object , dispatcher:EventDispatcher):Array
       {
           array.push(element);
           dispatcher.dispatch( new Event('Added Element') );
           return array;
       }
    

    实现实际上取决于您的环境。