0

Array.prototype.ownMethod = function(x) {
  x(this)
}

[1, 2, 3, 4].ownMethod(function(x) {
  return x[1]
}) // This callback won't work .

I tried to pass array value inside my First-class function. This method won't work for me.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335

1 Answers1

6

You've been caught out by Automatic Semicolon Insertion.

The function expression creates an object (the function) and the square bracket immediately after it is used as a property accessor instead of an array literal.

Add an explicit end of statement with a semi-colon to fix that.

Array.prototype.ownMethod = function(x) {
  x(this)
};

[1, 2, 3, 4].ownMethod(function(x) {
  console.log(x[1]);
}) 
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335