2

Possible Duplicate:
Static variables in JavaScript

I have a problem with javascript. What I intend to do is to simulate a class named Tooltip. Ex:

var Tooltip = function(){
    this.draw = .....;
    /////////// other members and methods

    this.Static = ...; // this one I want to be static }

The problem is I can't find a method to declare a static method/member. And I presume that the above way doesn't work. Is there any method to simulate a class with a static member? (preferably using object literals)

LE: I forgot to tell that I want to have acces to the static members from an instance of the class.

Community
  • 1
  • 1
user1552480
  • 207
  • 1
  • 3
  • 8

2 Answers2

3

Javascript is a Prototype language.. It's not C

That being said.. Watch some video's of David Crockford and see if this SO solution fit's your bill.

Good luck!!

Community
  • 1
  • 1
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
0

Here you go, tooltip.sam() will be "sam" and you can't change it.

This is called the "Revealing Module Pattern" I believe.

var tooltip = function() {
        var _sam = "sam";


        var sam : function()
           { 
             return _sam;
           }

        return { 
           sam :  sam 
        };
 })();
Hogan
  • 69,564
  • 10
  • 76
  • 117