This is a little obscure and not Unicode-related, so I'm leaving it outside the main article, but we can even create a custom class that inherits from String but with different iteration behavior, by overriding its Symbol.iterator method:
String
Symbol.iterator
class WeirdString extends String { isWeird = true ;*[Symbol.iterator]() { yield* this.split('_') } } const weirdStr = new WeirdString('aaa_b')
Here, we can see clearly the different behaviors at work:
weirdStr.split('') // ["a", "a", "a", "_", "b"] ;[...weirdStr] // ["aaa", "b"] Array.from(weirdStr) // ["aaa", "b"] Object.assign([], weirdStr) // ["a", "a", "a", "_", "b", isWeird: true]
We also see that the isWeird property is preserved when using Object.assign, because it's an enumerable own property.
isWeird
Object.assign
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is a little obscure and not Unicode-related, so I'm leaving it outside the main article, but we can even create a custom class that inherits from
String
but with different iteration behavior, by overriding itsSymbol.iterator
method:Here, we can see clearly the different behaviors at work:
We also see that the
isWeird
property is preserved when usingObject.assign
, because it's an enumerable own property.