问题
I have a java script class say "myjavascript.js". I have the following class
var myClass= function () {
this.property2 = '';
this.property3 = '';
this.property4 = '';
this.property5 = '';
this.say() = function () {
alert('Say Hello');
}
I have a function which is triggered on some event.
function myFunction(){
var myClassObj= new myClass();
myClassObj.property2 = 'property2' ;
myClassObj.property3 = 'property2' ;
myClassObj.property4 = 'property2' ;
myClassObj.property5 = 'property2 ';
myClassObj.say();
}
On triggering function I am getting the this error
Uncaught TypeError: undefined is not a function
Keep in mind both are in the same file.
回答1:
this.say()
is the error. You are calling the function, not defining it.
this.say = function () {
alert('Say Hello');
}
来源:https://stackoverflow.com/questions/26550413/how-to-initialize-a-class-object-in-javascript