javascript 'use strict' 和 Object.defineProperty setter
javascript 'use strict' and and Object.defineProperty setter
'use strict';
class DataClass{
constructor(name){
this.name = name;
Object.defineProperties(this, {
_archives :{
value : []
},
temperature : {
enumerable : true,
set : function(value){
// error message : InternalError: too much recursion
//this.temperature = value;
//error message in case of 'use strict' : ReferenceError: assignment to undeclared variable temperature
temperature = value;
this._archives.push(value);
},
get : function(){
return ' yyy ';
}
},
});
}
}
var dataClass1 = new DataClass('giovanni');
dataClass1.temperature = 45;
console.log(dataClass1.temperature);
当我尝试在严格模式下运行这段代码时,我总是得到这个错误ReferenceError: assignment to undeclared variable temperature
我想了解解决此问题的最佳方法
当您使用 'use strict' 时,您不能使用未声明的变量。
在 "this.name = name;" 之后放 "var temperature;"
'use strict';
class DataClass{
constructor(name){
this.name = name;
var temperature; // <- private variable, completely different from the public one.
Object.defineProperties(this, {
_archives :{
value : []
},
temperature : { // <- public variable Object.temperature, internally this.temperature
enumerable : true,
set : function(value){
// error message : InternalError: too much recursion
//this.temperature = value; <-- Call the set again, that's why the loop.
//error message in case of 'use strict' : ReferenceError: assignment to undeclared variable temperature
temperature = value;
this._archives.push(value);
},
get : function(){
return ' yyy ';
}
},
});
}
}
var dataClass1 = new DataClass('giovanni');
dataClass1.temperature = 45;
console.log(dataClass1.temperature);
'use strict';
class DataClass{
constructor(name){
this.name = name;
Object.defineProperties(this, {
_archives :{
value : []
},
temperature : {
enumerable : true,
set : function(value){
// error message : InternalError: too much recursion
//this.temperature = value;
//error message in case of 'use strict' : ReferenceError: assignment to undeclared variable temperature
temperature = value;
this._archives.push(value);
},
get : function(){
return ' yyy ';
}
},
});
}
}
var dataClass1 = new DataClass('giovanni');
dataClass1.temperature = 45;
console.log(dataClass1.temperature);
当我尝试在严格模式下运行这段代码时,我总是得到这个错误ReferenceError: assignment to undeclared variable temperature 我想了解解决此问题的最佳方法
当您使用 'use strict' 时,您不能使用未声明的变量。 在 "this.name = name;" 之后放 "var temperature;"
'use strict';
class DataClass{
constructor(name){
this.name = name;
var temperature; // <- private variable, completely different from the public one.
Object.defineProperties(this, {
_archives :{
value : []
},
temperature : { // <- public variable Object.temperature, internally this.temperature
enumerable : true,
set : function(value){
// error message : InternalError: too much recursion
//this.temperature = value; <-- Call the set again, that's why the loop.
//error message in case of 'use strict' : ReferenceError: assignment to undeclared variable temperature
temperature = value;
this._archives.push(value);
},
get : function(){
return ' yyy ';
}
},
});
}
}
var dataClass1 = new DataClass('giovanni');
dataClass1.temperature = 45;
console.log(dataClass1.temperature);