简体   繁体   English

JS返回功能不存在

[英]JS returning function not present

I have following code. 我有以下代码。

var handleJson = function(){
        var jsonData ='xx';
        var eventObject,eventObjectx=[];
        that = this;
        var convertJsonToObject = function(datax){
                //debugger;
                try {
                        that.printList(datax.data);
                    } catch(e) {
                      console.log(e);
                    }

                //debugger;
            }

        return{

            getDatafromserver: function(url){

                    $.ajax({
                        crossOrigin: true,
                        url: url,
                        success:convertJsonToObject
                            //console.log(jsonData);
                    });

            },

            printList:function(eventObject){

                $.each(eventObject,function(index,val){

                    $('#eventlist').append('<li>'+val.event+'</li>');
                })
            }

        }
    }


    var jsonHandler = new handleJson();

        jsonHandler.getDatafromserver(url);

        //jsonHandler.printList('eventlist');


});

Although the function printList exist its returning error 尽管函数printList存在,但返回错误

TypeError: that.printList is not a function {stack: (...), message: "that.printList is not a function"} TypeError:that.printList不是函数{stack:(...),消息:“ that.printList不是函数”}

Can any one help me out? 谁能帮我吗?

The value of this as of your that = this line is not in any way related to the object that you return at the end of your handleJson function. that = this行开始的this值与您在handleJson函数末尾返回的对象没有任何关系。

Since you're using new with handleJson , you want to add to the object that this refers to, rather than returning a new, unrelated object: 由于您使用newhandleJson ,您要添加的对象, this是指,而不是返回一个新的,不相关的对象:

var handleJson = function() {
    var jsonData = 'xx';
    var eventObject, eventObjectx = [];
    var that = this;
    var convertJsonToObject = function(datax) {
        //debugger;
        try {
            that.printList(datax.data);
        } catch (e) {
            console.log(e);
        }

        //debugger;
    }

    this.getDatafromserver = function(url) {

        $.ajax({
            crossOrigin: true,
            url: url,
            success: convertJsonToObject
                //console.log(jsonData);
        });

    };

    this.printList = function(eventObject) {

        $.each(eventObject, function(index, val) {

            $('#eventlist').append('<li>' + val.event + '</li>');
        })
    };

};

var jsonHandler = new handleJson();

jsonHandler.getDatafromserver(url);

//jsonHandler.printList('eventlist');

Here's how new works: 这是new工作原理:

  • It creates a new object backed by the object that the target function's prototype property refers to; 它创建一个新对象,该对象由目标函数的prototype属性引用的对象支持; so in your case, a new object backed by handleJson.prototype . 因此,在您的情况下,需要一个由handleJson.prototype支持的新对象。
  • It calls the target function ( handleJson ) with this referring to that new object. 它调用目标函数( handleJson用) this指的是新的对象。
  • When the target function returns, if it doesn't return anything, or it returns a primitive value, or it returns null , new takes the object that it created as its result; 当目标函数返回时,如果不返回任何内容,或者返回原始值,或者返回null ,则new将其创建的对象作为结果;否则,返回null but if the target function returns a non- null object reference, that overrides that default and new takes that object reference as its result. 但是,如果目标函数返回一个非null对象引用,则它将覆盖该默认值,而new将该对象引用作为其结果。

In your original code, that last point was coming into play. 在您的原始代码中,最后一点正在发挥作用。


Side note: Your code was falling prey to The Horror of Implicit Globals becuse you weren't declaring that . 旁注: 由于您没有声明that您的代码成为“隐式全球性恐怖”的牺牲品。 I've added var on it above. 我在上面添加了var


Side note: The overwhelming convention in JavaScript is that a constructor function (a function you're calling via new ) starts with a capital letter. 旁注:JavaScript中的压倒性约定是构造函数 (您通过new调用的函数)以大写字母开头。 They're also usually nouns rather than verbs (other kinds of functions are usually verbs, but not constructor functions, which are named after the thing they construct). 它们通常也是名词,而不是动词 (其他类型的函数通常是动词,但不是构造函数,后者以其构造的事物命名)。 So JsonHandler rather than handleJson . 所以是JsonHandler而不是handleJson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM