Recursion Too Big Error When Using Intanceof Over Typeof?

I made a function to convert all dates in an object to strings, and when I used the following function, I got an error in FF "too much recursion". (It also fails in IE and 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 did it work fine?

    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 used (in json form)

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 seems like when I delete the method parameters (init, update and load), it works with the instanceof example.

. ASP.Net ScriptManager. , , - ASP.Net. ASP.Net ScriptManager , for .

+3
1

(-, , FF 3.5.7, . , ?)

, :

(a) ? . o.f==o, o o.f, o ...

(b) ? obj instanceof Object , obj Object . , Javascript Object, , , . , if . , typeof obj == "object" , obj Object. , , obj String ( typeof obj String). , .

+4
source

Source: https://habr.com/ru/post/1729261/


All Articles