Reputation: 81
I am new to JS and I got the below code from one of the internet forums and it's not working if I invoke the function from the Javascript class like below. When I try to execute I get the below errors and I am unable to figure out whats the reason. Can someone please help me with what I am missing here?
var person1 = {
FirstName: "First Name1",
LastName: "Last name1",
Age: 30,
EMailAddresses: [
"[email protected]"
],
Children: [{
FirstName: "Child First Name1",
LastName: "Child Last Name1",
Age: 2
}, {
FirstName: "Child First Name2",
LastName: "Child Last Name2",
Age: 5
}]
};
var person2 = {
LastName: "LastName2",
Age: 33,
EMailAddresses: [
"[email protected]",
"[email protected]"
],
Children: [{
FirstName: "Child First Name1",
LastName: "Child Last Name1",
Age: 3
}, {
FirstName: "Micky",
LastName: "Mouse",
Age: 5
}]
};
class MasterClass {
async getDifferencs(objectA, objectB) {
var propertyChanges = [];
var objectGraphPath = ["this"];
(function(a, b) {
if (a.constructor == Array) {
for (var i = 0; i < a.length; i++) {
objectGraphPath.push("[" + i.toString() + "]");
arguments.callee(a[i], b[i]);
objectGraphPath.pop();
}
} else if (a.constructor == Object || (a.constructor != Number &&
a.constructor != String && a.constructor != Date &&
a.constructor != RegExp && a.constructor != Function &&
a.constructor != Boolean)) {
for (var property in a) {
objectGraphPath.push(("." + property));
if (a[property].constructor != Function) {
arguments.callee(a[property], b[property]);
}
objectGraphPath.pop();
}
} else if (a.constructor != Function) { // filter out functions
if (a != b) {
propertyChanges.push({
"Property": objectGraphPath.join(""),
"ObjectA": a,
"ObjectB": b
});
}
}
})(objectA, objectB);
return propertyChanges;
}
}
var my_class = new MasterClass();
var differences = my_class.getDifferencs(person1, person2);
console.log(differences);
Upvotes: 0
Views: 5435
Reputation: 1335
(function test() {
console.log( arguments.callee === test ); // true
})();
arguments.callee
, which refers to the function itself, is not available in strict mode. You may give a name to the function, and use that name as reference:
async getDifferencs(objectA, objectB) {
var propertyChanges = [];
var objectGraphPath = ["this"];
(function _self(a, b) { // <--- (1)
if (a.constructor == Array) {
for (var i = 0; i < a.length; i++) {
objectGraphPath.push("[" + i.toString() + "]");
_self(a[i], b[i]); // <--- (2)
objectGraphPath.pop();
}
} else if (a.constructor == Object || (a.constructor != Number &&
a.constructor != String && a.constructor != Date &&
a.constructor != RegExp && a.constructor != Function &&
a.constructor != Boolean)) {
for (var property in a) {
objectGraphPath.push(("." + property));
if (a[property].constructor != Function) {
_self(a[property], b[property]); // <--- (3)
}
objectGraphPath.pop();
}
} else if (a.constructor != Function) {
if (a != b) {
propertyChanges.push({
"Property": objectGraphPath.join(""),
"ObjectA": a,
"ObjectB": b
});
}
}
})(objectA, objectB);
return propertyChanges;
}
Upvotes: 2