Показать сообщение отдельно
Старый 16.06.2015, 10:41
rs7 вне форума Посмотреть профиль Отправить личное сообщение для rs7 Найти все сообщения от rs7
  № 1  
Ответить с цитированием
rs7
 
Аватар для rs7

Регистрация: Jun 2009
Сообщений: 56
По умолчанию Func.apply с сохранением контекста?

Есть способ вызвать функцию с массивом параметров, как Func.apply, но при этом сохранить изначальный контекст функции?

Наткнулся в проекте на вот такую вещь, это делающую. Неужели нельзя сделать красивее?

Код AS3:
    /**
     * Calls the given function with the arguments in the given array, similar to Function::apply(). In contrast to
     * apply(), splat() will preserve the `this' value of the function, if any.
     *
     * The arguments array cannot be longer than 5 elements.
     *
     * @param target The function to be called
     * @param args The arguments args to call it with
     * @return The return value of the function, if any
     */
    public function splat(target : Function, args : Array) : *
    {
        switch( args.length )
        {
            case 0:
                return target();
            case 1:
                return target(args[0]);
            case 2:
                return target(args[0], args[1]);
            case 3:
                return target(args[0], args[1], args[2]);
            case 4:
                return target(args[0], args[1], args[2], args[3]);
            case 5:
                return target(args[0], args[1], args[2], args[3], args[4]);
        }
    }