zoukankan      html  css  js  c++  java
  • js方法中的this

    比如有个function:

     1 function ServiceMy(services) {
     2     //存放this,用于调试用
     3     var tmp_this = this;
     4     this.services = [];
     5     console.log(typeof this);
     6     console.log(this.__proto__.constructor.name);
     7     console.log(this.services == services);
     8     console.log(services);
     9     console.log(this.services);
    10 
    11     /**
    12        注意myLog和myLog2这两个function的区别,myLog是全局的,
    13        即在window下定义了一个方法,方法名为myLog,所以即使在ServiceMy()外部也可以直接调用myLog()方法
    14        而myLog2这个方法实际上是在ServiceMy定义了一个属性,这个属性又是一个方法,
    15        相当于window.ServiceMy.myLog2=function(){},所以只能在ServiceMy内部调用,
    16        在ServiceMy这个方法内部,this就代表ServiceMy本身,所以在ServiceMy内部调用内部写好的方法可以使用"this."
    17        的形式进行访问调用。
    18        最终效果可以表示为:
    19        window.myLog = function(){};
    20        window.ServiceMy.myLog2 = function(){} ,这两者的差异一目了然
    21     */
    22     myLog = function(msg) {
    23         alert("myLog:" + msg);
    24         // console.log("控制台:"+msg);
    25     }
    26 
    27     this.myLog2 = function(msg) {
    28         alert("myLog2:" + msg);
    29     }
    30 
    31     this.appendServices = function(services) {
    32         if (!services) {
    33             return this;
    34         }
    35         //存放 appendServices方法的入参
    36         var tmp_services = services;
    37         if (!$.isArray(tmp_services)) {
    38             tmp_services = [tmp_services];
    39         }
    40 
    41         //循环入参
    42         for (index in tmp_services) {
    43             if (!tmp_services[index].serviceId || !tmp_services[index].method) {
    44                 alert('服务定义的入参必须有serviceId和method');
    45                 return;
    46             }
    47 
    48             if (tmp_services[index].parameters) {
    49                 if (typeof tmp_services[index].parameters != 'object' || $.isArray(tmp_services[index].parameters)) {
    50                     alert('服务定义的参数必须是map形式!');
    51                     return;
    52                 }
    53             }
    54 
    55         }
    56 
    57     };
    58     //在外部 如果用new的形式构造出一个ServiceMy对象,如果new的时候有参数,那么就调用ServiceMy.appendServices(para)方法
    59     if (services) {
    60         this.appendServices(services);
    61         myLog('heihi=id');
    62     }
    63 
    64     this.appendProc = function(proc) {
    65         if (!proc) {
    66             return this;
    67         }
    68 
    69         tmp_proc = proc;
    70 
    71         if (!$.isArray(tmp_proc)) {
    72             // tmp_proc = [tmp_proc];
    73         }
    74 
    75         procedure_ = []; //用于存放取出来的数组元素的
    76         for (var index in tmp_proc) {
    77             procedure = tmp_proc[index];
    78             if (!procedure.procName) {
    79                 console.log('你没有传入过程名');
    80                 // alert('你没有传入过程名');
    81                 // return false;
    82                 return this;
    83             }
    84             if (!procedure.parameters) {
    85                 console.log('你没有传入存储过程需要的参数');
    86                 // return false;
    87                 // alert('你没有传入存储过程需要的参数');
    88                 return this;
    89             }
    90             procedure_.push(procedure);
    91         }
    92 
    93     }
    94 
    95 }

    一个function ServiceMy()的内部的this,就代表这个function本身,具体可以看上面js代码关于myLog和myLog2的注释说明。

    关于myLog和myLog2的注释说明:

    注意myLog和myLog2这两个function的区别,myLog是全局的,
    即在window下定义了一个方法,方法名为myLog,所以即使在ServiceMy()外部也可以直接调用myLog()方法
    而myLog2这个方法实际上是在ServiceMy定义了一个属性,这个属性又是一个方法,
    相当于window.ServiceMy.myLog2=function(){},所以只能在ServiceMy内部调用,
    在ServiceMy这个方法内部,this就代表ServiceMy本身,所以在ServiceMy内部调用内部写好的方法可以使用"this."
    的形式进行访问调用。
    最终效果可以表示为:
    window.myLog = function(){};
    window.ServiceMy.myLog2 = function(){} ,这两者的差异一目了然

    具体代码:

    http://files.cnblogs.com/files/Sunnor/20160630%E6%B5%8B%E8%AF%95%E7%94%A8%E6%9D%A5%E6%B5%8B%E8%AF%95js%E4%B8%ADthis%E7%9A%84%E9%97%AE%E9%A2%98.rar

  • 相关阅读:
    WebPlayer9电影整站系统第三方电影批量添加工具
    Delphi执行SQL提示“不正常地定义参数对象”,“提供了不一致或不完整的信息”错误
    IIS控制大全
    Redis慢查询日志
    Redis短结构
    分布式AKF拆分原则
    Redis分片
    Redis的bitmap从基础到业务
    什么是CAP?
    Redis面试题
  • 原文地址:https://www.cnblogs.com/Sunnor/p/5629223.html
Copyright © 2011-2022 走看看