Cannot Set Property ... Which Only Has Getter (javascript es6)
There are a couple problems here.
When you make a getter via get x()
you are causing this.x
to result in calling the getter, which will recurse indefinitely due to your get x()
doing this.x
.
Replace your references to this.x
with this._x
in this code like this:
class MyClass {
constructor(x) {
this._x = x === undefined ? 0 : x;
}
get x() {
return this._x;
}
}
Now your encapsulated x
which is now _x
will not be confused for a call to the getter via this.x
.
Javascript won't let me set property which only has a getter despite deleting getter
As said in comments, you can't delete what isn't there. password
is not a property of user
, but of its prototype; so delete user.password
does nothing; if you then do user.password = "foo"
, you will find the property user
on the prototype, which is not settable.
Instead, you need to define a property on user
itself:
class User { get password() { Object.defineProperty(this, "password", { value: "foo" }); return this.password; }};
var user = new User();console.log(user.password);
Uncaught TypeError: Cannot set property playerNo of # which has only a getter on line 4
You have recursion with setting playerNo
.
In the playerNo
setter, try setting this._playerNo = 0
.
In the rest of the code, continue to make a distinction between the name of the setter method and the internal property that stores the data.
How to solve error TypeError: Cannot set property list of #<HTMLInputElement> which has only a getter?
That's because the "list" attribute is read-only and therefore you need to use the setAttribute function to set it/change it.
Try this:
const Input1 = document.createElement('input')
Input1.type = 'text'
Input1.setAttribute("list","car")
Why Memoization in Es6 classes with getters/setters is not working?
The problem is that delete obj[prop]
will only "delete" the property prop
if it's directly on obj
. It will not delete the property if the property is on the internal prototype. For another example:
const obj = Object.create({ prop: 'val' });
delete obj.prop;
console.log(obj);
Related Topics
What Does Webkitformboundary Mean
Merge Two Json/Javascript Arrays in to One Array
How to Send a Http Delete Request from Browser
Syntaxerror: Cannot Use Import Statement Outside a Module
Changing Svg Image Color With JavaScript
Passing Data from Node Js to HTML Using Ejs
Convert a Flat Json File to Tree Structure in JavaScript
How to Avoid Scientific Notation for Large Numbers in JavaScript
Regexp to Match Every Occurence After N Occurences
Disable Scrolling When Touch Moving Certain Element
How to Create Prompt With Two Input Fields
Javascript Loop Wait Until API Call Finished
Google Firestore - How to Get Several Documents by Multiple Ids in One Round-Trip