0

I'm trying to set a getter on some node so when its id/className/src attributes will be called my function will be called also, my code is:

 let RealClass = node.className;
 Object.defineProperty(node, "className", {
   get(value) {
      //some more code
      return RealClass
   }
 });

when i'm calling node.className i'm indeed getting the RealClass but when i'm trying to set the class nothing happens , why? i only defined a getter and not a setter... and if i need to define a setter also how do i call the original setter for the node because if i will define a setter like this:

 let RealClass = node.className;
 Object.defineProperty(node, "className", {
   set(value) {
      //some code
      this.className = value;
    }
  });

it will actually cause an infinite loop. ideas?

Oguntoye
  • 645
  • 1
  • 7
  • 19
avi dahan
  • 539
  • 3
  • 19
  • 1
    If you are using getters/setters you can'T use the same identifier for actual storing a value, so just store it ina n attribute like `this._className` – Sebastian Speitel Aug 13 '18 at 11:04
  • @SebastianSpeitel try to run this code and see what i mean , i don't need to store the value i need to add something to that value and just set the new value but for some reason even if i didn't defined a setter i cant set the node className – avi dahan Aug 13 '18 at 11:08
  • 1
    You are creating a setter for `className` (which is called when `className` is set to a new value) and in it you set the value itself, which calls the setter and so on. – Sebastian Speitel Aug 13 '18 at 11:11
  • @SebastianSpeitel you didn't read my question properly, i don't need a setter i'm only using a getter but for some reason when i'm trying to set the node className nothing happens! so i thought that i need to add a setter also thats all, i don't need to store any value i just need the original methods to work so when i will call node.className = "test" the className will actually change – avi dahan Aug 13 '18 at 11:16
  • 1
    You are not listening to your previous interlocutor, change `this.className = value;` into something like `this._className = value;` with an underscore , don't give it the same name as the property your defined. It causes an infinite loop because you are using the set(value) function in a recursive fashion. `this.className = value` will call `set(value){ /* ... */ }` – mpm Aug 13 '18 at 11:19
  • @mpm I can't see why this will help me , i need to change the node className not _className – avi dahan Aug 13 '18 at 11:22
  • 1
    Because you don't understand how `defineProperty` works at first place and you refuse to listen people who are pointing out your mistake. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty you just defined a property called `className`, you cannot set the value of `className` in the set function or it will call the set function recursively, you need to use another variable name. – mpm Aug 13 '18 at 11:23
  • You got an infinite loop because you do something wrong. These people above explain what you do wrong and what you can do to fix the issue. It's not that they don't understand your question. It's that you don't understand your problem. This fine, we all start somewhere but give these gentlemans advice a try. It wouldn't disappoint you. – Mark Baijens Aug 13 '18 at 11:36
  • @MarkBaijens don't get me wrong i really appreciate any help ! , but as i said i did tried that but i will just set another attribute and i saw this question: https://stackoverflow.com/questions/39560085/change-innerhtml-set-on-the-fly , and the solution there: var originalSet = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML').set; in this case the original setter is used so i thought i can do the same here but apparently not – avi dahan Aug 13 '18 at 11:49
  • In the question you linked the OP has an infinite loop problem too and the accepted answer doesn't not have this situation. It's often a bad idea to copy code from a question. There is often something wrong with it (that's why they have a question about it) – Mark Baijens Aug 13 '18 at 12:01

0 Answers0