JS Built-ins
Functioncallintermediate

Function.prototype.call() β€” borrow Array.push

πŸ“– Usage Example
function Point(x, y) { this.x = x; this.y = y }
const p = {}
Point.call(p, 1, 2)
p  // β†’ { x: 1, y: 2 }

call() is used to invoke a constructor function on an existing object.

Borrowing array methods with call()

You can borrow built-in array methods for array-like objects using call().

Challenge: Use [].push.call(arr, 10) to push 10 onto arr and verify arr[0] === 10.

πŸ’‘ Hints (1)
  • β€’ `[].push.call(target, value)` calls `Array.prototype.push` on `target`.