1

Recently, I have written this function for an OP:

$.prototype.reduce = function reduce(obj1, obj2) {
  for (var k in obj2) {
    if (obj1.hasOwnProperty(k) && obj2.hasOwnProperty(k)) {
      if (typeof obj1[k] == "object" && typeof obj2[k] == "object") {
         reduce(obj1[k], obj2[k]);
      }
      else delete obj1[k];
    }
  }
}

This function uses the feature of named functions expressions to make use of recurrence. I am now wondering, how to accomplish the same behavior using the new arrow function, as they are per definition always anonymous.

$.prototype.reduce = (obj1, obj2) => {
  for (var k in obj2) {
    if (obj1.hasOwnProperty(k) && obj2.hasOwnProperty(k)) {
      if (typeof obj1[k] == "object" && typeof obj2[k] == "object") {
         XXXX          }
      else delete obj1[k];
    }
  }
}

In place of XXX, I would have the following ideas:

1. arguments.callee(obj1[k], obj2[k]);(obviously deprecated)

2. $.reduce(obj1[k], obj2[k]) (calling the function variable itself - will fail for self-invoking function expressions)

3. Keep using "classical" function expression

Are there more approaches I missed? Which is the best way to use recurrence in double arrow functions?

Mulan
  • 129,518
  • 31
  • 228
  • 259
Liglo App
  • 3,719
  • 4
  • 30
  • 54
  • 1
    Yes, you should keep using normal function expressions, or use the variable that you assigned to. Another possibility would be the y-combinator as mentioned in the duplicate. – Bergi Dec 19 '14 at 13:00

0 Answers0