2

Hey I hava a function which acts like a class.

var myClass= function () {
        this.property = '';
        this.say() = function () {
            alert('Say Hello');
        }

When I initialize it like this

var myClassObj= new myClass();    
myClassObj.property = 'property';
myClassObj.say();

It gives me the error on initialization "Uncaught TypeError: undefined is not a function". Whats I am doing wrong.

2
  • 4
    this.say() = function => this.say = function. You don't want to invoke this.say immediately (which is undefined at this point). Commented Oct 27, 2014 at 6:43
  • this.say() tries to call this.say, but this.say does not exist. Commented Oct 27, 2014 at 6:45

2 Answers 2

3

Use this.say instead of this.say().

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

Comments

1

As hinted by Andrew, use this.say instead of this.say()

In your code you also miss closing } ,

It should be like this,

var myClass= function () {
    this.property = '';
    this.say = function () {
        alert('Say Hello');
        }
  }


var myClassObj= new myClass();    
myClassObj.property = 'property';
myClassObj.say();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.