Javascript对象和传递成员函数作为参数

时间:2012-02-16 02:20:42

标签: javascript function object arguments

这很难解释,如果我写一些示例代码可能会更好:

function A()
{
    this.b = new B();

    this.a_value = 456;

    this.f = function(i)
    {
        for(var i = 0; i < this.a_value; ++i)
            DoStuff(i);
    }

    this.b.C(this.f)
}

我正在尝试将函数作为参数传递给B,但是当C尝试达到a_value时,它是未定义的。我该如何解决?

我希望我没有过分简化我的问题。

4 个答案:

答案 0 :(得分:2)

您可以将this.f传递给c函数,但要正确调用它,您还需要传递this的值,如下所示:

function A()
{
    this.b = new B();

    this.a_value = 456;

    this.f = function(i)
    {
        for(var i = 0; i < this.a_value; ++i)
            DoStuff(i);
    }

    this.b.C(this.f, this)   // <== pass "this" here
}

And, then in `this.b.c`:

...b.c = function(fn, context) {
   fn.call(context);      // <== use .call() here to apply the right this value
}

答案 1 :(得分:2)

现在(或者从2015年开始),您还可以将函数绑定到对象实例,如图所示in this answer

public class FlowConfiguration extends AbstractFlowConfiguration {

    @Autowired
    Validator validator;
    @Bean
    public FlowDefinitionRegistry flowRegistry() {
        return getFlowDefinitionRegistryBuilder()
            .setFlowBuilderServices(flowBuilderServices())
            .setBasePath("/WEB-INF/flows")
            .addFlowLocationPattern("/**/*-flow.xml")
            .build();
    }
    @Bean
    public FlowExecutor flowExecutor() {
        return getFlowExecutorBuilder(flowRegistry())
                    .addFlowExecutionListener(securityFlowExecutionListener())
                    .build();
    }
    @Bean
    public FlowHandlerMapping flowHandlerMapping()
    {
        FlowHandlerMapping fh = new FlowHandlerMapping();
        fh.setFlowRegistry(flowRegistry());
        return fh;
    }
    @Bean
    public FlowHandlerAdapter flowHandlerAdapter()
    {
        FlowHandlerAdapter fh = new FlowHandlerAdapter();
        fh.setFlowExecutor(flowExecutor());
        return fh;
    }
    @Bean
    public FlowBuilderServices flowBuilderServices()
    {
        return getFlowBuilderServicesBuilder()
                .setValidator(validator)
                .build();
        /*FlowBuilderServices fbs = getFlowBuilderServicesBuilder().build();
        System.out.println(fbs.getApplicationContext());
        fbs.setApplicationContext(getApplicationContext());
        fbs.setValidator(validator);
        return fbs;*/

    }
    @Bean
    public SecurityFlowExecutionListener securityFlowExecutionListener()
    {
        return new SecurityFlowExecutionListener();
    }
}

作为解释:

this.b.C(this.f.bind(this));

答案 2 :(得分:1)

问题是this没有绑定到JavaScript中的任何固定值。你可以使用一个闭包来解决这个问题,在这种情况下它可能是最好的方法:

function A()
{
    var that = this;

    this.b = new B();

    this.a_value = 456;

    this.f = function(i)
    {
        for(var i = 0; i < that.a_value; ++i)
            DoStuff(i);
    }

    this.b.C(this.f);
}

答案 3 :(得分:0)

使用箭头功能实现相同功能的另一种方法。由于箭头函数不绑定this,因此它将保留定义函数时的任何值。

function A()
{
    this.b = new B();

    this.a_value = 456;

    this.f = i =>
    {
        for(var i = 0; i < this.a_value; ++i)
            DoStuff(i);
    }

    this.b.C(this.f)
}