Javascript: prototype method error?

1. This. TestFunc () That's fine.

That'll work, with the other calls removed (Well, it works as long as you don't peel off the RealFunc from its owner and call it on its own, like var method= myObj. RealFunc; method() or via an event handler or timeout. In that case this in RealFunc wouldn't be the MyObject instance and you'd need to look at closures or Function.

Bind to get it to work. ) 2. TestFunc () Nope, TestFunc is not defined as a variable in local or global scope.

This causes the error you get from Firebug 3. (I didn't really think this would work, // but thought it was worth a try...) MyObject. TestFunc () No, you were right.

:-) It would be MyObject.prototype.TestFunc. Call(this) done explicitly JavaScript does kind of confuse the matter by putting some of the same methods on the standard constructor functions for built-in objects as on their prototypes (for example String. Split exists where really only String.prototype.

Split should). But that doesn't happen to your own objects unless you explicitly say something like MyObject. TextFunc= MyObject.prototype.TextFunc.

1. This. TestFunc (); That's fine.

That'll work, with the other calls removed. (Well, it works as long as you don't peel off the RealFunc from its owner and call it on its own, like var method= myObj. RealFunc; method(); or via an event handler or timeout.In that case this in RealFunc wouldn't be the MyObject instance and you'd need to look at closures or Function.

Bind to get it to work. ) // 2. TestFunc (); Nope, TestFunc is not defined as a variable in local or global scope.

This causes the error you get from Firebug. // 3.(I didn't really think this would work, // but thought it was worth a try...) MyObject. TestFunc (); No, you were right.

:-) It would be MyObject.prototype.TestFunc. Call(this), done explicitly. JavaScript does kind of confuse the matter by putting some of the same methods on the standard constructor functions for built-in objects as on their prototypes (for example String.

Split exists where really only String.prototype. Split should). But that doesn't happen to your own objects unless you explicitly say something like MyObject.

TextFunc= MyObject.prototype.TextFunc.

Ah ha! I didn't include it in the above simplified version, since I totally forgot about what happens to 'this' in an eventhandler, but RealFunc () is indeed an event handler... – Paul Woolcock Feb 5 '10 at 16:10 Yeah, I suspected as much... the way method-binding and this works in JavaScript is very strange compared to other programming languages; it's probably the most common gotcha. – bobince Feb 5 '10 at 16:21.

Variant 1 seems correct. I tried your code in ExecuteJS and skipped 2. And 3.

, it worked (although I removed the call to console. Log and changed it to alert). TestFunc is called within RealFunc.

What happens if you remove "use strict";?

ECMAScript Fifth Edition's 'use strict' won't do anything... yet. I look forward to browsers being released where it does, though! – bobince Feb 5 '10 at 14:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions