tearsinchina
V2EX  ›  问与答

请教一个jquery的问题

  •  
  •   tearsinchina · Mar 7, 2012 · 4535 views
    This topic created in 5191 days ago, the information mentioned may be changed or developed.
    示意代码:

    uploadify = function (id){
    this.id = id;
    this.isSuccessful = false;
    }

    uploadify.prototype.initialize = function(){

    $("#" + this.Id).uploadify({

    ...................省略一堆代码....

    onUploadSuccess: function (file, data, response) {


    alert(this.isSuccessful ); /////出错代码

    },
    });
    }
    请教如何能够找到isSuccessful 这个属性使它不出错
    7 replies    1970-01-01 08:00:00 +08:00
    krazy
        1
    krazy  
       Mar 7, 2012
    var self = this;
    self.isSuccessful
    tearsinchina
        2
    tearsinchina  
    OP
       Mar 7, 2012
    @krazy var self = this;
    alert(self.isFinishConversion);


    这样写还是弹出undefined
    tearsinchina
        3
    tearsinchina  
    OP
       Mar 7, 2012
    @krazy 搞定了,把self=this 弄在最外面就行了

    请问这是什么原理
    tearsinchina
        4
    tearsinchina  
    OP
       Mar 7, 2012
    @krazy 还有个问题。我定义了个方法,像这样

    uploadify.prototype.showWrongMessage =function(file,msg){

    }

    onUploadError: function (file, errorCode) {
    switch (errorCode) {
    case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
    self.showWrongMessage(file, 'size or type is wrong');
    break;
    case SWFUpload.UPLOAD_ERROR.IO_ERROR:
    self.showWrongMessage(file, 'size or type is wrong');
    break;
    }
    },

    提示 "self.showWrongMessage is not a function",请问是为什么
    eric_zyh
        5
    eric_zyh  
       Mar 7, 2012
    1.通过原型(prototype)拓展的方法,对于不同对象会有不同的this。
    2.this.isSuccessful = false; 是定义在uploadify,对象的作用域并没有isSuccessful属性

    3."self.showWrongMessage is not a function",如果showWrongMessage是用prototype定义的,这个时候就要用 this.showWrongMessage了。
    tearsinchina
        6
    tearsinchina  
    OP
       Mar 7, 2012
    @eric_zyh

    第3点,因为showWrongMessage 是在一个jquery插件里面的方式调用的,所以this 得到的是这个插件的对象,而不是我定义的那个,因此会报找不到方法的错误
    tuoxie007
        7
    tuoxie007  
       Mar 7, 2012 via iPhone
    想知道this是什么,看看这个吧, http://tuoxie.me/blog/javascript-this-object-bind/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4314 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 01:01 · PVG 09:01 · LAX 18:01 · JFK 21:01
    ♥ Do have faith in what you're doing.