Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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.
DeprecatedI guess it's not a bug anymore.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question