最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How should I call a javascript class method from html? - Stack Overflow

programmeradmin3浏览0评论

I have a javascript class as follows:

function Apple (type) {
    this.type = type;
    this.color = "red";
}

Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
};

now I want to call method getinfo from my html file on an event. say I have a div like below:

<div ... onmouseover="getinfo()"></div>

now I know that above is not the proper method to call getinfo. So what is the best method to do it?

One way could be as below that I define a class object in windowonload() call and use the object to call the method getinfo(). But what if I have lot of classes for a big project and lot of objects. Then I need to define each object in windowonload() call which does not seem right to me. There must be a way to create objects on the fly. Kindly guide me?

I have a javascript class as follows:

function Apple (type) {
    this.type = type;
    this.color = "red";
}

Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
};

now I want to call method getinfo from my html file on an event. say I have a div like below:

<div ... onmouseover="getinfo()"></div>

now I know that above is not the proper method to call getinfo. So what is the best method to do it?

One way could be as below that I define a class object in windowonload() call and use the object to call the method getinfo(). But what if I have lot of classes for a big project and lot of objects. Then I need to define each object in windowonload() call which does not seem right to me. There must be a way to create objects on the fly. Kindly guide me?

Share Improve this question edited Jul 3, 2012 at 16:00 Guffa 701k111 gold badges756 silver badges1k bronze badges asked Jul 3, 2012 at 15:57 RaviRavi 3932 gold badges3 silver badges13 bronze badges 2
  • 1 It doesn't make any sense, you'd need to have an Apple object somewhere to call the method on – Esailija Commented Jul 3, 2012 at 16:01
  • Is each div supposed to have an Apple object associated with it? – lbstr Commented Jul 3, 2012 at 16:03
Add a ment  | 

2 Answers 2

Reset to default 2

Declare your class:

function Apple (type) {
    this.type = type;
    this.color = "red";
}

Apple.prototype.getInfo = function() {
    return this.color + ' ' + this.type + ' apple';
};

Create an instance of it:

var apple = new Apple("pink lady");

Then play with it:

<div ... onmouseover="alert(apple.getInfo())"></div>

To call the method you need to create an instance of the object, for example:

new Apple('Granny Smith')

You can keep the reference to the object in a variable, and use the variable to call the method:

var green = new Apple('Granny Smith');
var info = green.getinfo();

Or you can create the object on the fly and call the method directly:

var info = new Apple('Granny Smith').getinfo();

Generally it would make sense to create a instance of the object when the page loads, and use that later on.

发布评论

评论列表(0)

  1. 暂无评论