2

Suppose I need to declare some private static members to use it in some public static methods...

// ***** Variant I *****
function supports() {
    var impl = document.implementation; // private, non-static
    this.SVG = function () {            // public, non-static
      return impl.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1");
    };
}

// ***** Variant II *****
function supports() { }
supports.prototype.impl = document.implementation; // public, non-static
supports.SVG = function () {                       // public, static
  return impl.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1");
};

I know there are some difficulties in JavaScript with the 'static' OOP concept, so my question about is:

Can I declare public static methods inside the "declaration body" of the 'object' (like in the "Variant I" above)?

2
  • 2
    supports.prototype.impl = ... - public non-static (member) Commented Sep 18, 2015 at 10:07
  • @hindmost: thanks for correction! I fixed the comment accordingly Commented Sep 18, 2015 at 10:22

4 Answers 4

3

There is no private, static or public in JavaScript. There is only local and global, in-scope and out-of-scope. Use an IIFE to capture a variable and return a closure; this should work equivalently to a static method.

var supports = (function supportsWrapper() {
    var impl = document.implementation; // "static"-ish.
    return function supports() {
      this.SVG = function () {
        return impl.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1");
      };
    }
})();

impl will be initialised only once, and will be readable only by supports, but will persist between calls to it.

Sign up to request clarification or add additional context in comments.

2 Comments

how about to declare public static methods inside the "class body"?
There is no "class body" in JS (at least as of ES5). And if you are going by Java's definition of "static method", they're just functions defined on the constructor function. You will be much happier if you stop writing Java in JavaScript, and start writing JavaScript.
3

In your Variant II the function that is supposedly private and static is neither private nor static.

You are correct that it is quite difficult to define a private static member of the class, but it is possible if you exploit JavaScript's scope.

var Person = (function(){

    var privateStaticProperty = true;

    function privateStatic() {
        return privateStaticProperty;
    }

    function Person(){

        // class declarations
    }

    Person.prototype.example = function() {
        return privateStatic();
    }

    return Person;
}());

var p = new Person();
console.log(p.example());

Note, however, that if you extend the prototype outside of the closure, the supposedly private static members will not be available.

10 Comments

how about to declare public static methods inside the "class body"?
@Serge you have figured that out yourself in variant II supports.SVG is public and static. Make sure you keep that out of the class declaration (as is in your example), because everything that is inside will be executed each time the class is instantiated, which is also why it is used as a constructor.
So you confirm that in JavaScript is impossible to declare a "public static" function inside the 'class declaration', (like in C#, by ex.) it only should be wrapped in a anonymous function
@Serge it is possible, but it will be redefined each time you instantiate the class and that makes no sense. JavaScript does not have an object model like other programming languages like C++, C#, Java etc. Wait until you need a destructor :D It is called a class but it is no class, it is just a function, which is also an object, because functions are objects in JavaScript
@Serge I guess you are correct, so the best you can do is declare it outside.
|
1

In JavaScript, there is no term or keyword static, but we can put such data directly into function object (like in any other object).

Static methods

Static methods, just like variables, are attached to functions. They are used mostly for objects:

function Animal(name) {
  arguments.callee.count = ++arguments.callee.count || 1 

  this.name = name
}

Animal.showCount = function() {
  alert( Animal.count )
}

var mouse = new Animal("Mouse")
var elephant = new Animal("elephant")

Animal.showCount()  // 2

2 Comments

Is there a way to declare public static methods inside the "class body"?
0

The only solution I see after analyzing some answers is this one:

var Person = (function () {

    var _privateStatic = 'private static member';
    function privateStatic() {
        return _privateStatic + ' + private-s-function';
    }

    function Person() {

        this.publicNonStatic = 'public, non-static member';
        this.publicFunction = function () { 
            return this.publicNonStatic + '+ public non-static-function'; 
        }

        var _privateNonStatic = 'private, non-static member';
        function privateNonStatic() {
            return _privateNonStatic + " + private-ns-function"
        }

        this.publicStatic = Person.publicStatic = 'public static member';
        this.publicStaticFunction = Person.publicStaticFunction = function () {
            return Person.publicStatic + ' + public static function';
        }        

        // Accessible internal 
        var test = _privateNonStatic;
        test = _privateStatic;
        test = privateStatic();
        test = privateNonStatic();

        // other class declarations
    }
    return Person;
}());

// Accessible external members: 
var p = new Person();
console.log(p.publicFunction());
console.log(p.publicNonStatic);
console.log(p.publicStatic);
console.log(p.publicStaticFunction());
console.log(Person.publicStatic); 
console.log(Person.publicStaticFunction());

PS.

I remarked however that publicStaticFunction() becames accesible only after a new Person() declaration... so, before it they are not available:

// Accessible external members: 
console.log(Person.publicStatic);           // NO WAY
console.log(Person.publicStaticFunction()); // NO WAY

var p = new Person(); // lazy declaration 

console.log(Person.publicStatic);           // IS OK!
console.log(Person.publicStaticFunction()); // IS OK!

, so I suppose there is no way to achieve it INSIDE the function Person body, only inside the anonymous function that wraps the Person...

So the initial SVG sample should be like this:

var supports = (function(){
    function supports() { this.SVG = supports.SVG; } 
    supports.SVG = function () {
        return document.implementation.hasFeature("http://shortened", "1.1");
    };
    return supports;
}());

// usage of public, static method
if (supports.SVG()) {return 'supports SVG!';}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.