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:
- @How to solve eclipse error after setter and @ getter (@ data) annotation
- There is no getter for property named ‘id‘ in ‘class java.lang.Integer‘
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- Failed to load Main-Class manifest attribute from when the jar file is running
- When jar file is running: Failed to load Main-Class manifest attribute from ……Solution
- ERROR in static/js/app.xxxxxxx.js from UglifyJs Unexpected token: operator (>)
- The shell runs the class file and imports the jar package that the class depends on
- Error c2011: “a certain class”: redefinition of “class” type
- Attribute Error: ‘XXX‘ object has no attribute ‘module‘
- SCRIPT1002: syntax error File: 0. chunk.js ,SCRIPT5009: ‘Map‘ is undefined File: 0. chunk.js -Solutions
- Problems encountered after Maven installation or upgrade: could not find or load main class org.codehaus.plexus .class…..
- The differences between the equals method in the string class and the equals method in the object class
- ERROR in ./node_modules/css-loader/dist/cjs.js!./node_modules/less-loader/dist/cjs.js!./src/css/spec
- KeyError: “Can‘t open attribute (can‘t locate attribute: ‘nrx‘)“
- C++ Error: allocating an object of abstract class type
- On and off of timer in JS
- A method of collecting JS dynamic content by PHP
- Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
- Beautiful soup4 gets the value of the class property of the tag
- Node.js Medium package.json The difference between devdependences and dependencies