zoukankan      html  css  js  c++  java
  • Js Pattern

    bad code

    // BEFORE: 5 globals
    // Warning: antipattern
    
    // constructors
    function Parent() {}
    function Child() {}
    
    // a variable
    var some_var = 1;
    
    // some objects
    var module1 = {};
    module1.data = {a: 1, b: 2};
    
    var module2 = {};

    good code

    // AFTER: 1 global
    // global object
    var MYAPP = {};
    
    // constructors
    MYAPP.Parent = function () {};
    MYAPP.Child = function () {};
    
    // a variable
    MYAPP.some_var = 1;
    
    // an object container
    MYAPP.modules = {};
    
    // nested objects
    MYAPP.modules.module1 = {};
    MYAPP.modules.module1.data = {a: 1, b: 2};
    MYAPP.modules.module2 = {};

    Some of the properties you’re adding to the namespace may already exist, and you could be overwriting them.
    Therefore before adding a property or creating a namespace, it’s best to check first that it doesn’t already exist

    // unsafe
    var MYAPP = {};
    
    // better
    if (typeof MYAPP === "undefined") {
        var MYAPP = {};
    }
    
    // or shorter
    var MYAPP = MYAPP || {};
  • 相关阅读:
    Lucene综合案例
    Lucene 高级搜索
    Lucene 分词器
    Lucene 索引维护
    Lucene Field域类型
    Lucene入门
    Lucene介绍和全文检索流程
    数据查询方法
    序列化
    drf
  • 原文地址:https://www.cnblogs.com/davidgu/p/3333129.html
Copyright © 2011-2022 走看看