Search code examples
javascriptdomtokenlist

Javascript - DOMTokenList prototype


I have a pretty general question and I was hoping someone from the community can educate me.

I don't understand how DOMTokenList was developed. It appears as though classList for example is a setter but seems to have prototype functions that can be accessed off the setter.

ele.classList="class1 class2" <-- setter

ele.classList.add() <--prototype

I have an application where it would be highly convenient to have my setter property also have prototype functions like this, does anyone know how in the world DOMTokenList was developed?


Solution

  • It actually uses a setter and a getter. When you use ele.classList = "class1 class2" it triggers the setter.

    When you use ele.classList.add('class1') it first gets the classList object and then trigger the add method.