Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

javascript function object prototype

Why prototype function is not called .. when image is clicket?

Html Code :--

 <!DOCTYPE html>
 <html style="height: 100%;">
 <head>
 <script type="text/javascript" src="tt.js"></script>
 </head>
 <body>
 <p>This example calls a function which performs a calculation, and returns the result:</p>
 <p id="demo"></p>
 <input type="image" src="http://t2.gstatic.com/images?q=tbn:ANd9GcSTcJA5J-LOj0HOP1ZMzdSQIsxwuguFdtlesHqzU15W8TXx232pFg" onclick="myFunction('Info clicked')"/>
 <script>
 var a = new myFunction();
 document.getElementById("demo").innerHTML = a.k;
 </script>
 </body>
 </html>

java script :--

function myFunction(l) {
 this.k = "hello";
 alert(this.k);
 var t = this.temp(l);
 alert(t);
}
myFunction.prototype.temp = function(a)
{
 alert(a);
 return 10;
}

If i put inside html page body it works :--

<script>
var a = new myFunction();
document.getElementById("demo").innerHTML = a.k;
</script>

Answer*

Draft saved
Draft discarded
Cancel
3
  • What do you mean, "on the constructor"? Commented Aug 14, 2014 at 10:44
  • @Bergi — developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 14, 2014 at 10:45
  • Without new, this is not the constructor, and with new it's neither. So what do you mean? Calling a method of the instance from within the constructor is fine in general. Commented Aug 14, 2014 at 11:51

default

AltStyle によって変換されたページ (->オリジナル) /