如何在javascript中初始化一个类对象

ozi*_*zil 0 javascript javascript-events

我有一个java脚本类说"myjavascript.js".我有以下课程

var myClass= function () {
        this.property2 = '';
        this.property3 = '';
        this.property4 = '';
        this.property5 = '';

        this.say() = function () {
            alert('Say Hello');
        }
Run Code Online (Sandbox Code Playgroud)

我有一个在某个事件上触发的功能.

function myFunction(){
var myClassObj= new myClass();
myClassObj.property2 = 'property2' ;
myClassObj.property3 = 'property2' ;
myClassObj.property4 = 'property2' ;
myClassObj.property5 = 'property2 ';
myClassObj.say();
}
Run Code Online (Sandbox Code Playgroud)

在触发功能时,我收到此错误

未捕获的TypeError:undefined不是函数

请记住,两者都在同一个文件中.

Mār*_*dis 5

this.say()是错误.您正在调用该函数,而不是定义它.

 this.say = function () {
            alert('Say Hello');
        }
Run Code Online (Sandbox Code Playgroud)