OO Javascript: a good way to combine prototype inheritance with private vars?

In OO Javascript constructor constructor: Neoclassical versus prototype , I found out that designers using prototype inheritance can be 10 times faster (or more) than constructors called the neo-classicalclosure pattern proposed by Crockford in his book "Good Details" and presentations .

For this reason, it seems that the preference for prototype inheritance seems to be correct, in general.

Question Is there a way to combine prototype inheritance with a module template so that private variables can be used if necessary?

I think:

// makeClass method - By John Resig (MIT Licensed)
function makeClass(){
  return function(args){
    if ( this instanceof arguments.callee ) {
      if ( typeof this.init == "function" )
        this.init.apply( this, args.callee ? args : arguments );
    } else
      return new arguments.callee( arguments );
  };
}


// =======================================================

var User = makeClass();

// convention; define an init method and attach to the prototype
User.prototype.init = function(first, last){
  this.name = first + " " + last;
};


User.prototype.doWork = function (a,b,c) {/* ... */ }; 

User.prototype.method2= (function (a,b,c) {

    // this code is run once per class

    return function(a,b,c) {
        // this code gets run with each call into the method 
        var _v2 = 0;
        function inc() {
            _v2++;
        }

        var dummy = function(a,b,c) {
            /* ... */
            inc();
            WScript.echo("doOtherWork(" + this.name + ") v2= " + _v2);
            return _v2;
        };

        var x = dummy(a,b,c);
        this.method2 = dummy; // replace self
        return x;
    };

})();

This is not entirely correct. But this illustrates the point.

Is there a way to do this and is it worth it?

+3
4

, ,

... , , , , JavaScript. JavaScript , !

, , , , , , object.method, function.bind.

, ?

? - Java , Python: , , , , . JavaScript, .

this , :

var Thing= makeClass();
Thing.prototype.init= function(a) {
    this._a= a;
    this.showA= this.showA.bind(this);
};
Thing.prototype.showA= function() {
    alert(this._a);
};

thing= new Thing(3);
setTimeout(thing.showA, 1000); // will work as `thing` has its own bound copy of `showA`

(function.bind - JavaScript, Function.prototype, .)

, , , , , , , , , .

, this. , , . , :

var User= makeClass();
User.prototype.init= function(first, last){
    this.name= first+' '+last;
    this.method2= this._method2factory();
};
User.prototype._method2factory= function() {
    var _v2= 0;
    function inc() {
        _v2++;
    }

    return function method2(a,b,c) {
        /* ... */
        inc();
        WScript.echo('doOtherWork('+this.name+') v2= '+_v2);
        return _v2;
    };
};

, , this._v2 this._inc().

+5

, . , , , ...

function Foo () { /*constructor*/
    var counter = 0;  /* private variable */
    this.incrementCounter=function () {  /*privileged function */
        counter++
    }
    this.getCounter=function () {   /*privileged function */
        return counter;
    }

}
 /*public functions. Note: this pattern destroys constructor property. 
 Lesson: Don't depend on the constructor property! */
Foo.prototype = {
   toString: function () {
          var string = "";
          var count = this.getCounter();
          while(count--) {
             string+="*"
          }
          return string;
     }  

}

var bar = new Foo();
bar.incrementCounter();
bar.incrementCounter();
bar.toString();  /* in theory, this returns "**".. haven't tested code */
+1

You can take a look at https://github.com/riga/jclass

I think what you are looking for.

0
source

Personally, I prefer the following syntax:

var keyValueStore = (function() {
    // Singleton private properties
    var count = 0;

    var kvs = function() {
        // Instance private / privileged properties
        count++;
    };

    kvs.prototype = {
        // Instance public properties
        'data' : {},
        'get' : function(key) { return this.data[key]; },
        'set' : function(key, value) { this.data[key] = value; },
        'delete' : function(key) { delete this.data[key]; },
        'getLength' : function() {
            var l = 0;
            for (p in this.data) l++;
            return l;
        }
    };

    return  {
        // Singleton public properties
        'create' : function() { return new kvs(); },
        'count' : function() { return count; }
    };
})();

Using this syntax, you have a singleton object, the ability to create instances with the inheritance of prototypes, and the ability to define private properties at several levels.

You use it as follows:

kvs = keyValueStore.create();
kvs.set('Tom', "Baker");
kvs.set('Daisy', "Hostess");
var profession_of_daisy = kvs.get('Daisy');
kvs.delete('Daisy');
console.log(keyValueStore.count());
0
source

Source: https://habr.com/ru/post/1724143/


All Articles