-

JS匿名函数自调用(图文教程)

JS匿名函数自调用

代码部分

第一种方法:这种方法在web浏览器上可以正常运行,但是在nodejs里和asp里不可以使用

  • 代码

<script type="text/javascript">
;(function(root, factory) { 
    var template = factory(root);
   
        root.template = template;
     
}(this, function(root) {
    function template(tpl, data) { 
        return tpl;
    } 
    template.debug = function (option) {
        return "xiyueta.com";
    } 
    return template;
})); 
console.log( template("xiyueta.js") )
console.log( template.debug() )    
</script>



第二种方法:这种方法在web浏览器上可以正常运行,也可以在nodejs里和asp里运行

  • 代码
  • ASP代码

<script type="text/javascript">
function _template() {
    this.template = function(tpl, data) {
        return tpl;
    }
    this.debug = function(option) {
        return "xiyueta.com";
    } 
} 
var template = function(tpl, data) {
    return new _template().template(tpl,data);
};
 
console.log(template("xiyueta.js"));
</script>

<script language="javascript" runat="server">
function _template() {
    this.template = function(tpl, data) {
        return tpl;
    }
    this.debug = function(option) {
        return "xiyueta.com";
    } 
} 
var template = function(tpl, data) {
    return new _template().template(tpl,data);
};
response.write(template("aaa")) 
</script>


第三种方法:仿jQuery调用方法,xiyueta就是用这种方法做的,在web浏览器上和nodejs里和ASP里都可以使用

  • 代码

<script type="text/javascript">
 var _XIYUETA = function() {
     this.template = function(tpl, data) {
         return tpl;
     }

     this.debug = function(option) {
         return "xiyueta.com";
     }


 }
 
 var _xyt = new _XIYUETA();

 var xiyueta = function(config, x, y) {
     return new xiyueta.fn.init(config, x, y);
 };
 xiyueta.fn = xiyueta.prototype = {
     length: 0, //长度
     selector: "", //选择器
     domList: undefined, //找到标签列表,(这个暂时不改为[]20220415)
     x: null,
     y: null,
     domListIndex: 0  
 };
 //这个是引用jquery
 xiyueta.extend = xiyueta.fn.extend = function() {
     var options, name, src, copy, copyIsArray, clone,
         target = arguments[0] || {},
         i = 1,
         length = arguments.length

     if (i === length) {
         target = this;
         i--;
     }

     for (; i < length; i++) {
         if ((options = arguments[i]) != null) {
             for (name in options) {
                 copy = options[name];
                 if (name === "__proto__" || target === copy) {
                     continue;
                 }

                 if (copy !== undefined) {
                     target[name] = copy;
                 }
             }
         }
     }
     return target;
 };
 var init = xiyueta.fn.init = function(selector, x, y) {
     this.length = 0; //找到条数
     this.selector = selector; //选择器
     this.domList = []; //undefined 改为 []  因为它是数组,与jquery保持一致 20220415
     this.x = x;
     this.y = y;
     this.domListArray = []; //DOM列表数组,配合end()使用 20210925

 };

 init.prototype = xiyueta.fn;
 $ = $xyt = xiyueta;
 xiyueta.fn.extend({
     template: function(tpl, data) { //解析HTML标签  isRepair为是否修复HTML默认为true 20210909         
         return _xyt.template(tpl);
     }
 });

 xiyueta.template = function(tpl) {
     return _xyt.template(tpl);
 }

 console.log(xiyueta().template("xiyueta.js"));
 console.log(xiyueta.template("xiyueta.com"));
</script>