Skip to Content
author's profile photo Former Member
Former Member

How declare a static method?

Hi,

I was wondering for classes that extend Class: sap.ui.base.Object, how do you declare static methods?

For example, given the Dog class definition below, how would I add a static method getStaticBark() ?

I would like to call the static method as follows: my.Dog.getStaticBark();

///////////////////////////////////////////////////////////

//Dog Class Definition

sap.ui.base.Object.extend('my.Dog', {

getBark: function () {

return "bark";

},

});//end class

thanks for any help.

regards,

Phong

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Jan 06, 2014 at 11:51 AM

    Hi Phong

    Theres a few different ways you can create static methods, below is an example

    (function () {
    
        jQuery.sap.declare("my.Dog");
        jQuery.sap.require("sap.ui.base.Object");
    
        sap.ui.base.Object.extend('my.Dog', {
            getBark: function () {
                return "bark";
            },
        }); //End Class
    
    //Statics
        var _instance;
    
        my.Dog.getInstance = function () {
            if (!_instance) {
                _instance = new my.Dog();
              }
            return _instance;
        };
    
        my.Dog.getStaticBark = function () {
           this.bark = "woof woof"
           return this.bark;
        };
    })();
    
    
     jQuery.sap.require("my.dog");
    my.Dog.getInstance().getBark(); >> bark
    my.Dog.getStaticBark() >> woof woof
    
    
    
    
    

    see JS Bin example here

    Cheers

    JSP

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.