Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Static props and methods...

    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;

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can prototype new functions to existing objects:

    String.prototype.someNewFunctionality = function()
    {

    }

    Is that what you mean?
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Regular Coder
    Join Date
    Nov 2003
    Location
    Code Heaven
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you have:

    function MyConstructor() {
    //blablabla
    }

    Then why not just say:

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

    ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •