admin管理员组

文章数量:1401149

I'm trying to auto increment a properties value each time I instantiate a new instance of the class. This is what my class constructor looks (I abstracted it down just a bit):

var Playlist = function(player, args){
    var that = this;
    this.id = ?; //Should auto increment
    this.tracks = [];
    this.ready = false;
    this.unloaded = args.length;
    this.callback = undefined;
    this.onready = function(c){
        that.callback = c;
    };
    this.add = function(tracks){
        for(var i = 0; i < tracks.length; i++){
            this.tracks.push(tracks[i]);
            this.resolve(i);
        }
    };
    this.resolve = function(i){
        SC.resolve(that.tracks[i]).then(function(data){
            that.tracks[i] = data;
            if(that.unloaded > 0){
                that.unloaded--;
                if(that.unloaded === 0){
                    that.ready = true;
                    that.callback();
                }
            }
        });
    };
    player.playlists.push(this);
    return this.add(args);
};

var playlist1 = new Playlist(player, [url1,url2...]); //Should be ID 0
var playlist2 = new Playlist(player, [url1,url2...]); //Should be ID 1

I'd like to not define an initial variable that I increment in the global scope. Could anyone hint me in the right direction? Cheers!

I'm trying to auto increment a properties value each time I instantiate a new instance of the class. This is what my class constructor looks (I abstracted it down just a bit):

var Playlist = function(player, args){
    var that = this;
    this.id = ?; //Should auto increment
    this.tracks = [];
    this.ready = false;
    this.unloaded = args.length;
    this.callback = undefined;
    this.onready = function(c){
        that.callback = c;
    };
    this.add = function(tracks){
        for(var i = 0; i < tracks.length; i++){
            this.tracks.push(tracks[i]);
            this.resolve(i);
        }
    };
    this.resolve = function(i){
        SC.resolve(that.tracks[i]).then(function(data){
            that.tracks[i] = data;
            if(that.unloaded > 0){
                that.unloaded--;
                if(that.unloaded === 0){
                    that.ready = true;
                    that.callback();
                }
            }
        });
    };
    player.playlists.push(this);
    return this.add(args);
};

var playlist1 = new Playlist(player, [url1,url2...]); //Should be ID 0
var playlist2 = new Playlist(player, [url1,url2...]); //Should be ID 1

I'd like to not define an initial variable that I increment in the global scope. Could anyone hint me in the right direction? Cheers!

Share Improve this question asked Jan 29, 2016 at 23:34 cursedphilcursedphil 851 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use an IIFE to create a private variable that you can increment.

var Playlist = (function() {
  var nextID = 0;
  return function(player, args) {
    this.id = nextID++;
    ...
  };
})();

You can set Playlist.id = 0 somewhere in your code, then increment it in the constructor and assign the new value to the instance property, as: this.id = Playlist.id++.
This suffers from the fact that it is not well encapsulated, so it could be misused.

Otherwise, I was to propose the solution described by Mike C, but he already set a good answer that contains such an idea, so...

本文标签: Auto increment value in javascript classStack Overflow