Requirement : To be able to track string variables in javascript by assigning a unique ID to each variable.
Example:
var a = 'alkd';
var b = 'iquwq';
console.log(a.id); # 1234
console.log(b.id); # 5678
What I have tried : I have tried extending string object by adding a new method or property to 'String.prototype'
Output :
console.log(a.id); #1234
console.log(b.id); #1234 (returning same id as above)
What i have observed : Ever instance of a string is inherited from String.prototype. Changes to String.Prototype object are propagated to all String instances.
Example :
a.id = 8783; # setter
console.log(b.id)# returns 8783
My Questions :
- Is it actually possible to get required functionality by extending String Object?
- Is there an alternative way to do this?
- Are there any plans to add this functionality to future editions of ECMAScript?