Skip to content Skip to sidebar Skip to footer

How To Write Own Array Method In Javascript?

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

Solution 1:

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]);
}) 

Post a Comment for "How To Write Own Array Method In Javascript?"