mardi 31 mars 2015

How to track a string-variable in JavaScript

Requirement : To be able to track a 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 :



  1. Is it actually possible to get required functionality by extending String Object ?

  2. Is there any alternative way to do this ?

  3. Are there any plans to add such functionality to future editions of ECMAScript ?


Aucun commentaire:

Enregistrer un commentaire