Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mootools类,未定义json处理/函数_Javascript_Json_Mootools - Fatal编程技术网

Javascript Mootools类,未定义json处理/函数

Javascript Mootools类,未定义json处理/函数,javascript,json,mootools,Javascript,Json,Mootools,如果你在这里看到一些你认为可以改进的东西,请告诉我 问题是:我试图通过一个mootools类来完成处理一些Json数据的基本结构。我当前遇到的是当我调用'this.processObj'时,我收到'processObj未定义' 代码: 请求的onComplete中this的绑定是请求实例本身,而不是原始范围 您可以使用3种模式 1.保存引用 2.绑定匿名函数 您可以使用function.bind decorator更改回调中的作用域: getObj: function (callThis) {

如果你在这里看到一些你认为可以改进的东西,请告诉我

问题是:我试图通过一个mootools类来完成处理一些Json数据的基本结构。我当前遇到的是当我调用'this.processObj'时,我收到'processObj未定义'

代码:


请求的
onComplete
this
的绑定是请求实例本身,而不是原始范围

您可以使用3种模式

1.保存引用 2.绑定匿名函数 您可以使用function.bind decorator更改回调中的作用域:

getObj: function (callThis) {
    new Request.JSON({
      method: 'get',
      url: callThis,
      onRequest: function () {
        console.log('Loading: ' + callThis);
      },
      onComplete: function(thisObj){
        //console.log(thisObj);
        this.processObj(thisObj);
      }.bind(this) // change scope, inner this becomes the outer this as well.
    }).send();  
},
3.移动到类上的方法并直接绑定 这将跳过anon函数的创建

getObj: function (callThis) {
    new Request.JSON({
      method: 'get',
      url: callThis,
      onRequest: function () {
        console.log('Loading: ' + callThis);
      },
      onComplete: this.processObj.bind(this)          
    }).send();  
},
就偏好而言:我会选择#1,因为它的性能足迹最小,它是首选的mootools方式[tm]。那么,最好的代码组织可能是#3


还有第四种方法与mootools中的
绑定
类mutator结合使用更多,但我们不要这样做:)

非常完整的答案,我得到了1和ahum 4的混合,我知道绑定使用分析器进行了大量处理,所以我想我对doPerfect进行了一些重写,非常感谢Dimitar也要感谢您在您的网站上提供的所有信息!老实说,我学到了很多!不用担心,我喜欢它,因为它对我也有帮助-看到100个对我来说是新的问题,新模式,新想法,记录所有这些。。。
getObj: function (callThis) {
    new Request.JSON({
      method: 'get',
      url: callThis,
      onRequest: function () {
        console.log('Loading: ' + callThis);
      },
      onComplete: function(thisObj){
        //console.log(thisObj);
        this.processObj(thisObj);
      }.bind(this) // change scope, inner this becomes the outer this as well.
    }).send();  
},
getObj: function (callThis) {
    new Request.JSON({
      method: 'get',
      url: callThis,
      onRequest: function () {
        console.log('Loading: ' + callThis);
      },
      onComplete: this.processObj.bind(this)          
    }).send();  
},