2

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.

3
  • 1
    Your 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? Commented Oct 3, 2013 at 6:25
  • Yah in my case its working what i have suggested Commented Oct 3, 2013 at 6:26
  • I am getting error Uncaught TypeError: Object #<Object> has no method 'memberFun' Commented Oct 3, 2013 at 6:28

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ohh.. srry I was using that only..just typing mistake..but it's not working
@CodeCrypt otherwise it is working fine... see the added fiddle... if you look at the browser console it is printing both the messages
0

dont use ClassName instead

var ob = new ClassName();
ob.memberFun1();

See this mozilla tutorial

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.