Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by benjtupas

I've been using the keyword get and set of JavaScript but I don't think that I'm implementing it correctly. I think there's a problem when implemented inside an ImpactJS class. Here's a snippet of the code:

ig.module( 
    'core.data.slotgamedata' 
).requires(

).defines(function(){

    // Create a new singleton SlotGameData
    SlotGameData = ig.Class.extend({

        staticInstantiate: function() {
            if( SlotGameData.instance == null ) {
                return null;
            }
            else {
                return SlotGameData.instance;
            }
        },

        init : function() {
            SlotGameData.instance = this
        },
        
        _maxWinnings: -1,
        get maxWinnings() {
            return this._maxWinnings;
        },
        setMaxWinnings : function( value ) {
            this._maxWinnings = value;
        }
    });
   
   SlotGameData.instance = null;
});

I've done series of tests and the results are not as expected.

console.log( this.sgd.maxWinnings ); // returns -1
console.log( this.sgd.setMaxWinnings( 999 ) ); // returns undefined and should 999
console.log( this.sgd.maxWinnings ); // returns -1 and should 999

1 decade ago by vincentpiel

If you look at ig.Class, inside impact.js, you'll see that all operations deals with property as standard properties, ignoring the more recent get/set functions.
The code was taken from John resig, is like ten years old or so now -it is outdated-.

To understand a bit more, let look at one copy loop of ig.copy for instance (used inside ig.Class) :

            var c = {};
            for( var i in object ) {
                c[i] = ig.copy(object[i]);
            } 

if i (...) is the name of a property that is in fact a get/set property, then what will be done with
c[i] = ig.copy(object[i]) ;

is that the *value* of the getter will be copied into a standard property. Both setter and getter are lost.

You have several choices :
- Make a feature request, and wait for it to be implemented. Based on past experience, i would bet that you could be listened to quickly, in less than two years.
- Implement yourself the proper handling of getters/setters. It is a little work. ( I did it, unfortunately i cannot share since my version of impact is no more impact compliant now (too many changes)... )

But if i look to the code you posted : why use impact classes ?? just use standard javascript classes, more performant and memory efficient. And in this case (singleton), you don't even need a class, just an object, so keep things simple, you can just define a SlotGameData object in your module and things will work.
Page 1 of 1
« first « previous next › last »