I am new to javascript, and I have defined class like the following way-
function ClassName()
{
//Some code here
}
ClassName.prototype.memberFun = function(){
alert("I'm in memberFun()");
}
ClassName.prototype.memberFun1 = function(){
alert("I'm in memberFun1()");
//Trying to call above function like
this.memberFun();
}
Now, I am creating the object and calling the function here-
var ob = new ClassName();
ob.memberFun1();
But it is not working. I am getting an error saying-
Uncaught TypeError: Object #<Object> has no method 'memberFun'
Any help will be appreciated.
asked Oct 3, 2013 at 6:20
CodeCrypt
7112 gold badges8 silver badges20 bronze badges
-
1Your code should work. There is nothing wrong with it. When I paste it in the the Chrome console I get your two alerts. What is not working?Michael Geary– Michael Geary2013年10月03日 06:25:53 +00:00Commented Oct 3, 2013 at 6:25
-
Yah in my case its working what i have suggestedManish Kumar– Manish Kumar2013年10月03日 06:26:46 +00:00Commented Oct 3, 2013 at 6:26
-
I am getting error Uncaught TypeError: Object #<Object> has no method 'memberFun'CodeCrypt– CodeCrypt2013年10月03日 06:28:07 +00:00Commented Oct 3, 2013 at 6:28
2 Answers 2
javascript does not have a variable type like ClassName in java... all variables are declared using var
var ob = new ClassName();
ob.memberFun1()
Demo: Fiddle
answered Oct 3, 2013 at 6:21
Arun P Johny
389k68 gold badges533 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
CodeCrypt
Ohh.. srry I was using that only..just typing mistake..but it's not working
Arun P Johny
@CodeCrypt otherwise it is working fine... see the added fiddle... if you look at the browser console it is printing both the messages
answered Oct 3, 2013 at 6:23
Manish Kumar
10.6k26 gold badges83 silver badges159 bronze badges
Comments
lang-js