admin管理员组

文章数量:1432178

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("", "1.1");
    };
}

// ***** Variant II *****
function supports() { }
supports.prototype.impl = document.implementation; // public, non-static
supports.SVG = function () {                       // public, static
  return impl.hasFeature("", "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)?

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/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/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)?

Share Improve this question edited Sep 18, 2015 at 10:29 serge asked Sep 18, 2015 at 10:01 sergeserge 15.3k41 gold badges143 silver badges236 bronze badges 2
  • 2 supports.prototype.impl = ... - public non-static (member) – hindmost Commented Sep 18, 2015 at 10:07
  • @hindmost: thanks for correction! I fixed the ment accordingly – serge Commented Sep 18, 2015 at 10:22
Add a ment  | 

4 Answers 4

Reset to default 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/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.

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.

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

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!';}

本文标签: oopDeclare a public quotstaticquot function inside a JavaScript classStack Overflow