V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tearsinchina
V2EX  ›  问与答

请教一个jquery的问题

  •  
  •   tearsinchina · 2012-03-07 17:56:17 +08:00 · 3873 次点击
    这是一个创建于 4441 天前的主题,其中的信息可能已经有所发展或是发生改变。
    示意代码:

    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 条回复    1970-01-01 08:00:00 +08:00
    krazy
        1
    krazy  
       2012-03-07 18:08:47 +08:00
    var self = this;
    self.isSuccessful
    tearsinchina
        2
    tearsinchina  
    OP
       2012-03-07 18:15:37 +08:00
    @krazy var self = this;
    alert(self.isFinishConversion);


    这样写还是弹出undefined
    tearsinchina
        3
    tearsinchina  
    OP
       2012-03-07 18:19:42 +08:00
    @krazy 搞定了,把self=this 弄在最外面就行了

    请问这是什么原理
    tearsinchina
        4
    tearsinchina  
    OP
       2012-03-07 18:24:46 +08:00
    @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  
       2012-03-07 18:35:11 +08:00
    1.通过原型(prototype)拓展的方法,对于不同对象会有不同的this。
    2.this.isSuccessful = false; 是定义在uploadify,对象的作用域并没有isSuccessful属性

    3."self.showWrongMessage is not a function",如果showWrongMessage是用prototype定义的,这个时候就要用 this.showWrongMessage了。
    tearsinchina
        6
    tearsinchina  
    OP
       2012-03-07 18:44:35 +08:00
    @eric_zyh

    第3点,因为showWrongMessage 是在一个jquery插件里面的方式调用的,所以this 得到的是这个插件的对象,而不是我定义的那个,因此会报找不到方法的错误
    tuoxie007
        7
    tuoxie007  
       2012-03-07 20:37:55 +08:00 via iPhone
    想知道this是什么,看看这个吧, http://tuoxie.me/blog/javascript-this-object-bind/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2284 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:06 · PVG 13:06 · LAX 22:06 · JFK 01:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.