4

Please refer - https://jsfiddle.net/53ranmn5/1

Array.prototype.method1 = function() {
console.log("method1 called");
}
[1,2,3,4].method1();

I get the following error,

TypeError: Cannot read property 'method1' of undefined

Why so? How can I fix this?

rlemon
  • 16,698
  • 11
  • 85
  • 121
gopal rao
  • 293
  • 1
  • 11
  • 1
    https://jsfiddle.net/53ranmn5/1/ – gopal rao Apr 23 '15 at 14:42
  • Is the above working for you? – gopal rao Apr 23 '15 at 14:42
  • 1
    Your code works just fine, although `console.log([1,2,3,4].method1());` prints out undefined (in your fiddle) since method1 does not return any string itself. – JuniorDev Apr 23 '15 at 14:43
  • 1
    You've included what you tried, but not what you expect or what went wrong. – ssube Apr 23 '15 at 14:43
  • What was your expected output. The actual output is method1 called undefined method2 called Which is what I would expect. – bhspencer Apr 23 '15 at 14:44
  • 2
    @gopalrao My bad. Your code has a real problem. You should use `;` after the function definition. JavaScript treats `function () {..}[1, 2, 3,4]` as a Single expression. And since it returns `undefined`, you are getting the error. – thefourtheye Apr 23 '15 at 14:46
  • You guys are too quick to close, he's getting an undefined error, try running it yourself. OP is looking for `Object.defineProperty`[as seen here](https://jsfiddle.net/Kredit/53ranmn5/2/). – Scott Apr 23 '15 at 14:47
  • @ScottKaye Nope, check my previous comment. – thefourtheye Apr 23 '15 at 14:48
  • Still doesn't make sense to close so quickly - I didn't see his error as immediately obvious either. – Scott Apr 23 '15 at 14:50
  • 1
    @ScottKaye That is why I apologized and voted to reopen. – thefourtheye Apr 23 '15 at 14:50
  • @ thefourtheye anyway it should be closed as simple typo errors, but strange why js not worked automatic insertion of semicolons to end statements. – Grundy Apr 23 '15 at 14:51
  • @ScottKaye if you see fiddle from OP not in comment - all working – Grundy Apr 23 '15 at 14:53
  • 1
    @Grundy Hmmm, Is it really a typo? I have seen code where people don't use `;`s at all. I think this question deserves a decent explanation. – thefourtheye Apr 23 '15 at 14:53
  • I thought the typo flag was more suited for things like `var test = 3; console.log(tset)`, I'd say this is more of a "feature" of Javascript going wrong. – Scott Apr 23 '15 at 14:53
  • 1
    @Grundy Right, but it works for the wrong reason - having `console.log()` triggers Javascript to put an automatic semicolon after the prototype definition, which allows the rest of the code to work. – Scott Apr 23 '15 at 14:56
  • Regardless of the problem with the code, the question has a slew of problems on its own. The question was closed and downvoted because it does not show *any* effort nor does it contain a minimal example, desired behavior, and actual behavior. Using the example to reproduce some error and editing that into the question -- without knowing if it is the same error the OP was seeing -- is questionable, at best. – ssube Apr 23 '15 at 15:04

2 Answers2

10

You're missing a semicolon:

Array.prototype.method1 = function() {
    console.log("method1 called");
}; // <--- Hi there!
[1,2,3,4].method1();

What?

Semicolons are optional in javascript, so the code you wrote is equivalent to:

Array.prototype.method1 = function() { ... }[1,2,3,4].method1();
// after evaluating the comma operator:
Array.prototype.method1 = function() { ... }[4].method1();
// naturally, functions don't have a fourth index
undefined.method1();
// Error :(

Be careful with your semicolons!

Some reading material:

Community
  • 1
  • 1
Zirak
  • 35,198
  • 12
  • 75
  • 89
0

Works fine for me, just added one character:

Array.prototype.method1 = function() {
    console.log("method1 has been called");
};
[1,2,3,4].method1();
Beri
  • 10,277
  • 3
  • 24
  • 47
  • 1
    Please explain why the "one character" is necessary here. – thefourtheye Apr 23 '15 at 15:11
  • Your code is correct, when run under console. This bug must belong to jsfiddle alone. Compare it with this: http://stackoverflow.com/questions/948358/adding-custom-functions-into-array-prototype – Beri Apr 23 '15 at 15:15
  • 1
    @Beri When run inside a console, statement by statement, the bug isn't there because the array isn't there to confuse the js engine into thinking it's a property access. – Zirak Apr 23 '15 at 15:35