var s = "Hi";
Date.now = function (){
return new Date();
}
s.createdOn = function (){
return new Date();
}
alert(s.createdOn()); // This is not working
alert(Date.now()); // This works fine
Am i violating any rule. Because i can add new property to Date class but not to string class. why?
The reason you can’t add properties or methods to a string literal is that when you try to access a literal’s property or method, the Javascript interpreter temporarily copies the value of the string into a new object and then use that object’s properties or methods. This means a String literal can only access a string’s default properties or methods and those that have been added as prototypes.
More info can be obtained from this link:
http://www.hunlock.com/blogs/The_Complete_Javascript_Strings_Reference
Hope this will help you