MVC Ideas & how global variables become local variables 1 MVC thought
VC First Edition
!function(){ var view = document.querySelector(‘xxx‘)var controller = function(view){ ..… }controller.call(null,view)}.call()
VC Second Edition, addinit
!function(){ var view = document.querySelector(‘xxx‘)var controller = {view: null,init: function(view){ this.bindEvents(). //this.bindEvents().call(this)},bindEvents: function(){ ...… }} controller.init(view). //controller.init.call(controller,view)}.call()
MVC idea full version
!function(){ //M 数据相关的操作,负责和server数据交互var model = { fetch: function(){ ... }, save: function(){ ... }} //V 负责视图展示var view = document.querySelector(‘xxx‘) //C 负责逻辑等其他var controller = { view: null, model: null, init: function(view,model){ this.view = view this.model = model this.bindEvents() }, bindEvents: function(){ ... } } controller.init(view,model)}.call()
How do global variables in the 2 function become local variables?
Global variables will harass each other. So don't use global variables in your code. Only functions have global variables before ES6.
- How does a global change to a local variable?
- Put the surrogate-in a function like, and then. Call () Execute this function? Is that OK?
- No way. The function name is also a global variable (global function).
- So-drop the function name and turn the function into an anonymous function? Again function () {}.call () executes immediately, so it can, but chrome bugs, syntax error.
- How to change local variables by global variables:
- Method One:!function () {}.call ()
(The front plus + 、-、! can, this method will change the function's return value, but do not care about the function of the return value of the addition of a reverse does not matter)
- Method Two: (function () {}). Call ()
(Enclose the function in parentheses.) However, this is not recommended, because if the previous line (function) is added to a xxx, it is easy to be misunderstood by the browser to be in XXX (). )
MVC Ideas & how global variables become local variables