0
  1. In ES5 I create a Model-View-Controller structure by using IIFEs and reveal module pattern.

    var model = function () { ... }()
    var view = function () { ... }()
    var controller = function (model, view) { 
        view.functionname(var one);
        ......
    }(model, view)
    

The new block scope {} in ES6 can replace IIFEs, but how we call the functions/methods of the model/view from the controller ?

  1. To combine multiple javascripts in ES5 and avoid naming collision I use an expression:

    ;( code ...)
    

How can be this done in ES6 ?

3
  • 1
    view.function(var one); is invalid JS. ;( code ...) does not avoid naming collision. It may solve issues with automatic semi-colon insertion. Not sure what you are asking. Also, limit your question to one question. Commented Jun 15, 2017 at 10:19
  • Why do you think calling view.functionname(…) becomes something else in ES6? Commented Jun 15, 2017 at 10:36
  • I updated the code, it was just as a placeholder, because I wanted to understand the concept; (code) I just wanted to say that I force the evaluation of what is inside Commented Jun 15, 2017 at 10:38

2 Answers 2

1

The new block scope in ES6 can replace IIFEs

Not really. It can replace IIFEs that were merely introducing scope, but it cannot replace the module pattern - a block has no return value. Also it doesn't take any arguments. You could just use global variables:

var model, view, controller;
{
    model = {…};
}
{
    view = {…};
}
{
    let one = …;
    view.functionname(one);
    controller = {…};
}

But honestly that's quite weird1. There's no reason not to use the exact same revealing module pattern that we know since ES3.

1: A viable alternative would be ES6 modules, which allow circular dependencies and have better syntax for all this

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe something like this?

class View {
    exampleMethod() {
        console.log("view's example method called")
    }
}

class Model {
    exampleMethod() {
        console.log("model's example method called")
    }
}

class Controller {
    constructor(view,model) {
        this.view = view
        this.model = model
        console.log("Test")
    }
    exampleMethod() {
        this.view.exampleMethod()
        this.model.exampleMethod()
    }
}

const myView = new View()
const myModel = new Model()

const myController = new Controller(myView,myModel)
myController.exampleMethod()

To avoid name collision in ES6 you could wrap everything into

(()=>{
    // Code...
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.