S
S
sombressoul2013-10-24 01:08:33
Google Chrome
sombressoul, 2013-10-24 01:08:33

Disappearing getter - bug or feature?

Actually, instead of a thousand words:

var TestObj                     = {};
    TestObj.Trigger             = false;
    TestObj.SomeValue           = undefined;
    //TestObj._SomeValue_buffer   = undefined; /* если раскомментить, всё будет ок; или если удалить "TestObj._SomeValue_buffer = false;" в коде ниже; */

var SomeValue_setter    = function( v ){
    if ( TestObj.Trigger ){
        TestObj._SomeValue = v;
    } else {
        TestObj._SomeValue_buffer = v;
    }
};

var SomeValue_getter = function( v ){
    
    if ( TestObj._SomeValue === undefined ){
        TestObj._SomeValue = 10;
    }
    
    return TestObj._SomeValue;
};

TestObj.__defineSetter__( 'SomeValue' , SomeValue_setter );
TestObj.__defineGetter__( 'SomeValue' , SomeValue_getter );

TestObj.SomeFunction = function(){

    TestObj.Trigger = true;
    
    if ( TestObj._SomeValue_buffer ){
        TestObj.SomeValue = TestObj._SomeValue_buffer;
        TestObj._SomeValue_buffer = false; /* если грохнуть эту строку - всё будет ок; или если раскомментить "TestObj._SomeValue_buffer = undefined;" в коде выше; */
    }
    
}

TestObj.SomeValue = 15; /* инициализируем TestObj._SomeValue_buffer */

console.log( 'Before: ' );
console.log( TestObj.SomeValue ); /* 10 - ok */
console.log( TestObj._SomeValue ); /* 10 - ok */
console.log( TestObj._SomeValue_buffer ); /* 15 - ok */
console.log( 'setter/getter: ' + !!TestObj.__lookupSetter__( 'SomeValue' ) + '/' + !!TestObj.__lookupGetter__( 'SomeValue' ) ); /* true/true - ok */


TestObj.SomeFunction();

console.log( 'After: ' );
console.log( TestObj.SomeValue ); /* UNDEFINED - упс!.. должно быть 15 */
console.log( TestObj._SomeValue ); /* 15 - ok */
console.log( TestObj._SomeValue_buffer ); /* false - ok */
console.log( 'setter/getter: ' + !!TestObj.__lookupSetter__( 'SomeValue' ) + '/' + !!TestObj.__lookupGetter__( 'SomeValue' ) ); /* true/FALSE - упс!... испарился геттер */

// Chrome: 30.0.1599.101 m

The same code under FF 24.0 works quite predictably and the getter does not disappear anywhere. It could be attributed to the scope of variables (TestObj._SomeValue_buffer is used only in the setter and is not declared in the header), but we still explicitly refer to the TestObj namespace, and this disappearing getter ...
In general, if anyone knows why it works exactly like this, and not otherwise - explain, please. If this is still my mistake, and not a bug, of course.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sigod, 2013-10-24
@sombressoul

You've added too much here.
It's not the assignment that kills the getter _SomeValue_buffer, but the fact that the input type is different from the current one. Try TestObj.SomeValue = 15;to do it afterwards TestObj.SomeValue = true; // or anything except numbers- the getter immediately falls off. Try to do it differently, for example, immediately assign true, and then anything other than boolean - the getter is gone again.
I just went to MDN to read about __defineGetter__, and there are warnings:

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Deprecated
This feature has been removed from the Web. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
I guess it's not a bug anymore.
PS Judging by the variable names, is your main language C#?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question