zoukankan      html  css  js  c++  java
  • 前端

    1.this

    ------------------------------------
    1.
    function func() {
            //this,代指 window
        } 
        
        func();
    
    2.
    function Func() {
            this.name = '葫芦娃'; //this,代指 obj
        }
    
        obj = new Func();
        console.log(obj.name);
    
    3.
     function Func() {
            this.name = '葫芦娃'; //this,代指 obj
            this.show = function(){
                console.log(this);
            }
        }
    
        obj = new Func();
        console.show();
                
    
    4.
    userInfo = {
           name:'大宝',
           age:18,
           show:function() {
               console.log(this)  //userInfo
           }
    
       };
    
       userInfo.show()
    
    #-------结论:函数被 对象.函数  执行,那么函数中的this 就是 该对象   --------#
    
    
    加强版:
    1.
    userInfo = {
           name:'小黄人',
           age:20,
    
           show:function(){
               console.log(this);       //代指 userinfo
               (function(){
                   console.log(this);   //代指 window
               })()
           }
       };
    
       userInfo.show()
    
    2.
     function Func(){
          this.name = '小黄人';
          this.show = function(){
              console.log(this);        //Func----(obj)
              (function () {
                  console.log(this);    //window
              })()
          }
      }
    
      obj = new Func();
      obj.show()
      
    
    3.
    userInfo = {
          name:'小黄人',
          age:20,
          show:function(){
              console.log(this);       //userInfo
              
              var that = this;        //!!!!!!
              (function(){
                 console.log(that);   //userInfo    
              })()
          }
          
      };
      
      
        userInfo.show()
    示例
  • 相关阅读:
    进度条4
    构建之法读书笔记02
    进度条3
    个人作业四则运算2测试
    个人作业3
    从小工到专家阅读笔记01
    个人冲刺07
    个人冲刺06
    个人冲刺05
    学习进度条08
  • 原文地址:https://www.cnblogs.com/zhaochangbo/p/7867089.html
Copyright © 2011-2022 走看看