The mechanical problem with this design is that primitives (like strings, numbers, and booleans) cannot have their own properties. You could add a property to the primitive's associated prototype, like String.prototype.doSomething = function() { ... }
, which would allow you to do "foo".doSomething()
. (This is discouraged, though, since you should not modify objects you don't own.) However, you cannot attach a value to a specific primitive string value, which means you cannot implement data
and name
how you'd like.
Instead, the best option is likely to use an object with a toString
method that will allow you to stringify the object to a string of your desired form. This won't show in console.log
form, but it would if you added an empty string to it, like console.log(""+person)
.
If you want to give nightmares to anyone who maintains your code in the future, you could achieve this by having your object include RegExp.prototype
in its prototype chain. I strongly, actively discourage you from doing so, but suppose I'm not legally capable of stopping you, either; whether or not it should be, (ab)using RegExp.prototype
in this way is not (yet) a crime in any jurisdiction.