jasonsalas
IS-IT--Management
I'With the AJAX craze of late I'm doing a lot more OOP-style JavaScript. I'd like to know how to create/call Java/C#-style static methods (shared methods for the VB'ers) in JavaScript. I know how to use prototypes to create methods within defined classes, like so:
// constructor
function Person() {
this.firstName = 'foo';
this.lastName = 'bar';
}
// method
Person.prototype.getName = function() {
return this.firstname + ', ' + this.lastName;
}
...and then instantiate a Person object:
var p = Person();
alert('My Person object: ' + p.getName);
...but I'd like to be able to call methods without having to create instances of the class, like so:
alert(Person.getName());
Basically, what the Math class does. This is easy to do in Java and C#, merely marking any method as 'static'. Thanks for your help!
// constructor
function Person() {
this.firstName = 'foo';
this.lastName = 'bar';
}
// method
Person.prototype.getName = function() {
return this.firstname + ', ' + this.lastName;
}
...and then instantiate a Person object:
var p = Person();
alert('My Person object: ' + p.getName);
...but I'd like to be able to call methods without having to create instances of the class, like so:
alert(Person.getName());
Basically, what the Math class does. This is easy to do in Java and C#, merely marking any method as 'static'. Thanks for your help!