You can use the GET and SET keywords inside the “class” to set the save and accessor functions for a property and intercept the access behavior of that property.
class A {
constructor () {
this._a = 'a'
}
set a (val) {
if (Object.prototype.toString.call(val) !== '[object String]') {
this._a = 'error'
} else {
this._a = val
}
}
get a () {
return this._a
}
}
let obj = new A()
console.log(obj.a) // a
obj.a = 123
console.log(obj.a) // error
Read More:
- Exploring the streaming call of underscore
- Error c2064: term does not evaluate to a function in VC
- Access characteristics of construction methods in Java inheritance
- Brief introduction of idea Lombok and solutions for reporting red and wrong
- C++ Error: allocating an object of abstract class type
- Copy forbidden in C + +++
- Macro generated_ UCLASS_ Body () and generated_ Analysis of body ()
- How to handle exception in springboot
- OpenGL learning notes and other learning thinking
- error: invalid use of non-static member function
- [Qt] error: call to non-static member function without an object argument
- Override the equals method and override the toString method
- On the problem of QT program open crash
- How to call stored procedure in Hibernate
- Abstract precautions
- Server object error ‘ASP 0178: 80070005’
- Error reporting when Lombok @ data and @ builder are used together
- On the problem that VTK + QT process can’t end after closing the program
- Solution to the flashback of visual studio output window
- How do I change the default background color of all FIGURE objects created in MATLAB