MVC Ideas & how global variables become local variables

Source: Internet
Author: User

MVC Ideas & how global variables become local variables 1 MVC thought
  1. VC First Edition

    !function(){ var view = document.querySelector(‘xxx‘)var controller = function(view){    ..…   }controller.call(null,view)}.call()     
  2. 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()         
  3. 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?
    1. Global variables will harass each other. So don't use global variables in your code. Only functions have global variables before ES6.

    2. 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.
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.