What is the right way to wire together 2 javascript objects?

前端 未结 6 1905
梦如初夏
梦如初夏 2020-12-28 19:34

I\'m currently facing a conundrum: What is the right way to wire together 2 javascript objects?

Imagine an application like a text editor with several different fi

6条回答
  •  不思量自难忘°
    2020-12-28 19:39

    I would say, just wire them together:

    function wireTogether() {
      var v = new View();
      var c = new Controller();
      c.setView(v);
    }
    

    But then of course another question raises - how do you test the wireTogether() function?

    Luckily, JavaScript is a really dynamic language, so you can just assign new values to View and Controller:

    var ok = false;
    
    View.prototype.isOurMock = true;
    Controller.prototype.setView = function(v) {
      ok = v.isOurMock;
    }
    
    wireTogether();
    
    alert( ok ? "Test passed" : "Test failed" );
    

提交回复
热议问题