oop - "Friend Classes" in javascript - Stack Overflow

I have a Factory class that creates a Widget object. The Factory object needs to callback a "priva

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 e 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.

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 e 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.

Share edited Aug 23, 2017 at 19:58 user663031 asked Jun 21, 2011 at 10:36 DergDerg 3013 silver badges9 bronze badges 3
  • 4 Javascript is javascript, not Ada or C++ or Java. You can do what the language allows you, but you should always ask yourself whether you're not making it plicated just for the sake of being plicated. There really is no harm in just going for the simples solution. It's a scripting language, not a military tool. – entonio Commented Jun 21, 2011 at 10:45
  • though I totally agree with @entonio, here, I do not see any pitfalls. This code seems good to go. – mihsathe Commented Jun 21, 2011 at 10:49
  • 1 @mihsathe writing code like that will lead to problems down the line. it works, but it's not a remended style – Raynos Commented Jun 21, 2011 at 10:59
Add a ment  | 

2 Answers 2

Reset to default 2
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];
}

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

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;
        }
    }
}

I'm not familiar enough with object oriented JavaScript (I use mostly one-or-two liners inside GWT code) to actually give an Real Answer (But I found that my response were a bit long for a ment...)

I think self-modifying classes, sounds like a major potential for gotcha's.

I personally prefer languages such as JavaScript, Ruby, etc. that are not restrictive in what you can do (even if I have to use Java+GWT at work, hehe), but where you rely self discipline to not do stupid things. I would rather prefix the method name with "_" (and simply avoid using it where I should not), than try to enforce private methods. Since JavaScript by nature is very unrestricted in what crazy things you may do, it requires a lot of discipline anyway.

If you deleted a method after use; to kind-of-protecting it, could you not just as easily add a new method to do the same? You would still rely on your (and others) self discipline and sanity anyway, aren't you?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071284a4609562.html

相关推荐

  • oop - &quot;Friend Classes&quot; in javascript - Stack Overflow

    I have a Factory class that creates a Widget object. The Factory object needs to callback a "priva

    2小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信