简体   繁体   English

使用intanceof over typeof时“太多递归”错误?

[英]“Too much recursion” error when using intanceof over typeof?

I was making a function to convert all dates in an object to strings and when I used the following function I got a error in FF "too much recursion". 我正在创建一个函数将对象中的所有日期转换为字符串,当我使用以下函数时,我在FF中出现“太多递归”的错误。 (It also fails in IE and chrome) (IE和Chrome也失败了)

    function datesToString(obj) {
        if (obj instanceof Object) {
            if (obj instanceof Date) {
                obj = "this part does not matter";
            } else {
                for (var key in obj) { obj[key] = datesToString(obj[key]); }
            }
        }
        return obj;
    }

but when I changed it to use typeof it worked fine? 但当我改变它使用typeof它工作正常?

    function datesToString(obj) {
        if (obj  && typeof obj == "object") {
            if (obj instanceof Date) {
                obj = "this part does not matter";
            } else {
                for (var key in obj) { obj[key] = datesToString(obj[key]); }
            }
        }
        return obj;
    }

It should be the same amount of recursion. 它应该是相同的递归量。 Am I missing something? 我错过了什么吗? Why am I getting an error with the first example but not the second? 为什么我在第一个例子中遇到错误而不是第二个例子?

Update: here is an example of the object I was using (in json form) 更新:这是我正在使用的对象的一个​​示例(以json形式)

Contact = {
    "LContactID": "00000000-0000-0000-0000-000000000000",
    "CellPhone": "",
    "HomePhone": "4564564560",
    "OtherPhone": "",
    "BirthDate": new Date(-62135575200000),
    "SSN": "456456456",
    "HowToContact": 1,
    "ContactType": 3,
    "Address1": "123 test",
    "Address2": "apt. 1",
    "City": "Valparaiso",
    "State": "IN",
    "Zip": "46383",
    "FullAddress": "123 test, apt. 1",
    "BuilderID": "",
    "FullAddressWithCityState": "123 test\r\napt. 1\r\nValparaiso, IN 46383",
    "WorkHours": "9-5",
    "ContactTime": "",
    "ContactMethod": "???",
    "IsMilitaryOrSelfEmployed": false,
    "AlternateContactName": "",
    "AlternateContactPhone": "",
    "ContactID": "00000000-0000-0000-0000-000000000000",
    "Password": null,
    "FirstName": "updatetest",
    "MiddleName": null,
    "LastName": "test_",
    "Suffix": null,
    "EmailAddress": null,
    "EmailAddressAlternate": null,
    "WorkPhone": "6546546540",
    "WorkPhoneExt": null,
    "CreatedOn": new Date(1263597309000),
    "CreatedOnOverride": new Date(-62135575200000),
    "ModifiedOn": new Date(1264014749000),
    "SCRep": "",
    "HBCRep": "",
    "PPPRep": "",
    "DPSRep": "",
    "DRSRep": "",
    "RDRep": "",
    "OwnerID": "00000000-0000-0000-0000-000000000000",
    "FullName": "updatetest test_",
    "ImportID": 0,
    "IncludeEmail": false,
    "PreferredLanguage": 1,
    "CarbonCopyList": [],
    "HearAboutUs": 5,
    "HearAboutUsOther": "",
    "init": function() { },
    "update": function() { },
    "load": function() { }
}

It looks like when I remove the method parameters (init,update and load) it works with the instanceof example. 看起来当我删除方法参数(init,update和load)时,它与instanceof示例一起使用。

UPDATE: This is a ASP.Net ScriptManager problem it turns out. 更新:这是一个ASP.Net ScriptManager问题。 Sorry for not including the fact that I was working with an ASP.Net website.The ASP.Net ScriptManager prototypes methods to Function which causes an infinity loop when doing a recursive for in loop on a function. 很抱歉不包括我正在使用ASP.Net网站的事实.ASP.Net ScriptManager将函数原型化为函数,当对函数进行循环递归时会导致无限循环。

(First, let me say that I tried your code on FF 3.5.7 and it didn't run into an infinite recursion. maybe you used a different input example?) (首先,让我说我在FF 3.5.7上尝试了你的代码并且它没有遇到无限递归。也许你使用了不同的输入示例?)

Anyway, to answer your questions: 无论如何,回答你的问题:

(a) Why do you get an infinite recursion? (a)为什么你得到无限递归? Because an object may refer to itself. 因为对象可能指自己。 If of==o then calling your function on o will fire a subsequent call on of which is actually o and so on... 如果of==o然后调用你的函数o将触发对后续调用of实际上是o等等...

(b) Why the difference between the two versions? (b)为什么两个版本之间存在差异? obj instanceof Object checks if obj is an instance of Object or an instance of a subclass thereof. obj instanceof Object检查obj是Object的实例还是其子类的实例。 Given that every Javascript object inherits from Object then this condition is trivially true, and is thus meaningless. 鉴于每个Javascript对象都继承自Object,那么这种情况很简单,因此毫无意义。 Net result is that the first if always succeeds. 净结果是第一个if总是成功的话。 On the other hand typeof obj == "object" checks that obj is an instance of Object. 另一方面, typeof obj == "object"检查obj是Object的实例。 This may be false, for example, if obj is an instance of String (at which case typeof obj yields String ). 这可能是错误的,例如,如果obj是String的实例(在这种情况下, typeof obj产生String )。 Hence, the difference. 因此,差异。

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

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