如何在javascript中初始化一个类对象 [英] how to initialize a class object in javascript

查看:87
本文介绍了如何在javascript中初始化一个类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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不是函数

Uncaught TypeError: undefined is not a function

请记住两者在同一个文件中。

Keep in mind both are in the same file.

推荐答案

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

this.say() is the error. You are calling the function, not defining it.

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

这篇关于如何在javascript中初始化一个类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆