...

View Full Version : Static props and methods...



Code Wizard
12-30-2003, 01:50 PM
Using JavaScript's object-oriented paradigm how can I create static methods (and properties)?
That us methods that no instance of the object may call....these methods can only be called by the object prototype.
Examples:
Math.round();
Date.parseTime();
String.fromCharCode();
Number.NaN;

brothercake
12-30-2003, 02:39 PM
You can prototype new functions to existing objects:

String.prototype.someNewFunctionality = function()
{

}

Is that what you mean?

Code Wizard
12-31-2003, 01:26 PM
hmm let's see here:
function Person(name,age,sex)
{
this.name=name
this.age=age
this.sex=sex
}

//..... Some code here........

Person.prototype.display=somefunction;
Yeah but this will apply to instances..I want a method which I can only call by the constructor..like this:
Person.doTask();
and another example:
num=new Number(12);
Number.NaN is correct
num.NaN is NOT correct...
Me hopes u go it :)

jkd
12-31-2003, 01:59 PM
If you have:

function MyConstructor() {
//blablabla
}

Then why not just say:

MyConstructor.myStaticMethod = function() {
// foo!
}

?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum