“朋友班"在JavaScript中 [英] "Friend Classes" in javascript

查看:92
本文介绍了“朋友班"在JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建Widget对象的Factory类. Factory对象需要在稍后的时间回调Widget对象的私有方法",以向其传递一些ajax信息.到目前为止,我想出的唯一实现是在Widget中创建一个公共方法,该方法将私有方法返回给工厂,然后删除自身,然后Factory返回返回新的Widget,同时保留指向私有方法的指针.这是一个简化的示例:

I have a Factory class that creates a Widget object. The Factory object needs to callback a "private method" of the Widget object at a later time to pass it some ajax info. So far, the only implementation I've come up with is to create a public method in the Widget that returns the private method to the factory, and then deletes itself, the Factory then returns the new Widget while retaining a pointer to the private method. Here is a simplified example:

function Factory()
{
    var widgetCallback = null;

    this.ajaxOperation = function()
    {
        //some ajax calls
        widgetCallback('ajaxresults');
    }

    this.getNewWidget = function()
    {
        var wid = new Widget();
        widgetCallback = wid.getCallback();
        return wid;
    }

    function Widget()
    {
        var state = 'no state';
        var self = this;
        var modifyState = function(newState)
        {
            state = newState;
        }

        this.getState = function()
        {
            return state;
        }

        this.getCallback = function()
        {
            delete self.getCallback;
            return modifyState;
        }
    }
}

是否有更好的方法来实现我所追求的效果,或者这是一个相当合理的方法?我知道它能奏效,只是好奇我是否要介入任何我应该意识到的陷阱.

Is there a better way to achieve the effect I'm after or is this a fairly reasonable approach? I know it works, just curious if I'm stepping into any pitfalls I should be aware of.

推荐答案

this.getNewWidget = function() {
    var val = new Widget(),
        wid = val[0],
        widgetCallback = val[1];

    return wid;
}

function Widget() {
    var state = 'no state';
    var self = this;
    var modifyState = function(newState) {
        state = newState;
    }

    this.getState = function() {
        return state;
    }

    // Return tuple of Widget and callback
    return [this, modifyState];
}

只需让您的构造函数返回Tuple<Widget, function>

Just get your constructor to return a Tuple<Widget, function>

替代方法只是使用闭包作用域直接在Widget构造函数中编辑widgetCallback

Alternative just use closure scope to edit widgetCallback directly in your Widget constructor

function Factory() {
    var widgetCallback = null;

    this.ajaxOperation = function() {
        //some ajax calls
        widgetCallback('ajaxresults');
    }

    this.getNewWidget = function() {
        return new Widget();;
    }

    function Widget() {
        var state = 'no state';
        var self = this;
        // set it directly here!
        widgetCallback = function(newState) {
            state = newState;
        }

        this.getState = function() {
            return state;
        }
    }
}

这篇关于“朋友班"在JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆