[Javascript] IIFE

Javascript modules are a design pattern that allow you to encapsulate your code into smaller self managing pieces. They help you separate your code, protect your variables, and provide an easy way to expose only the parts of your component that you want to be exposed.

var APP = (function(){
    var vm = {};
    
    vm.output = 7;
    vm.doWork = function(){
       //...
    }        

    return vm;    
}());

To access the attr and method:

APP.ouput;

APP.doWork();
原文地址:https://www.cnblogs.com/Answer1215/p/4597739.html