zoukankan      html  css  js  c++  java
  • 解决js绑定事件this指向发生改变的问题

    可对函数进行如下扩展

     

     

     Function.prototype.bind = function(obj) {
    
        var _this = this;
    
        return function() {
    
            _this.apply(obj,arguments);
    
        }
    
    }


     

    用法如下

     

     

    var a = function(){
    
        alert(this.title)
    
    }.bind(document);
    
    a();


     

    常用在这儿

     

    function myalert() {
    
        this.title = 'hello world';
    
        this.init = function() {
    
               $("#xxx").click(this.close.bind(this));
    
        }
    
        this.close = function() {
    
            alert(this.title)
    
        }
    
    }

    var a  = new myalert();

    a.init();


     


  • 相关阅读:
    day66
    1
    day65
    BeautifulSoup
    day60
    day59
    day49
    day48
    [S5PV210] PWM
    [S5PV210] Clock
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3037434.html
Copyright © 2011-2022 走看看