English 中文(简体)
(3) 非指定职能协调人
原标题:as3 non-explicit function pointer

请参看以下守则。 我将拥有一组内容,我想就这一要素处理任何“形式”的提法。 在《法典》中,它说“这项工作”是按预期进行的。 然而,我需要放弃这些公式,而不明确指明“第一要素”。 尽管 lo虫是一小cl,但我认为它应该发挥作用,但造成以下错误。 在没有明确点名要素的情况下,我怎么能够击退公式呢? 感谢!

    var test:Object = { 
        element:
        [
            { "firstElement":
                {   
                    formula:myFunction
                }
            }
        ]
    }// end test object


    public function RunThisFunctionFirst() {

        test.element[0].firstElement.formula();//this works 

        for (var index in test.element){
            for (var object in test.element[index]){
                trace ("object " + object);// traces "firstElement", as expected
                object.formula()// this causes error: Error #1006: value is not a function.
            }
        }
    }

    function myFunction (){
        trace ("my function called");

    }
最佳回答

Regarding the outer loop, element is an array, not an object, so you want to use for(;;) not for in.

关于内 lo,object 是>string firstElement"而不是标的。

for (var i:int=0; i < test.element.length; i++)
{
    for (var key:* in test.element[i])
    {
        trace("key " + key);
        var object:* = test.element[i][key];
        trace("object " + object);
        if(typeof object === "object" && object.hasOwnProperty("formula"))
            object.formula();
    }
}
问题回答

每一条使用<代码> lo你可以简化 lo,正如先前所说,不要忘记打上你变数:

for each (var elm:Object in test.element) {
    for each (var obj:Object in elm) {
        var formula:Function = obj.formula as Function
        if (formula!=null) formula()
    }
}

Your variable object, in (var object ... ) is not a typed variable. The compiler will default this to an Object class, which of course is not a Function class. Try casting object as a Function. I m guess that you have extended myFunction from Function class.

either by: for (var object:Function in test.element[index])

or for (var object:myFunction ... ) // if myFunction is extended from Function





相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签