CodeCrypt
CodeCrypt

Reputation: 711

How can we pass parameters in javascript class methods

I have created a class in javascript, and I want to have methods as member of that class with parameter. How can we do? I have done following code in order to create a class and methods -

function Classname(){
   a1 = 100;
   this.a2 = 0;
}

Classname.prototype.method1 = function (message) {
   alert(message);
};

//Creating object
var obj1 = new Classname();

But when I am calling the method as -

obj1.method1("Hello");

Then I am getting error that Uncaught ReferenceError: messgae is not defined.

Upvotes: 1

Views: 5107

Answers (2)

ComFreek
ComFreek

Reputation: 29424

Uncaught ReferenceError: messgae is not defined

Are you sure that you use exactly the same code in your environment as you provided in the question?

You obviously use a name which can't be resolved due to a typo in the name (messgaemessage).

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129792

Your code works. Is it a verbatim copy?

Your error message seems to reference a misspelled variable, messgae.

Upvotes: 1

Related Questions