admin管理员组文章数量:1327661
I'm wondering why it seems that adding a method to the prototype of a string literal seems to work, but adding a property does not? I was playing with ideas in relation to this question, and have the following code:
String.prototype._str_index1 = 0;
String.prototype._str_reset = function() {
this._str_index1 = 0;
};
String.prototype._str_substr = function(len) {
var ret = this.substr(this._str_index1, len);
this._str_index1 = this._str_index1 + len;
return ret;
};
var testString = new String('Loremipsumdolorsitamet,consectetur');
log(testString._str_substr(5));
log(testString._str_substr(4));
This works fine. If however I change the third-last line to:
var testString = 'Loremipsumdolorsitamet,consectetur';
...it seems that although the method _str_substr
exists and is callable on the string literal, the value of the property _str_index1
is always 0.
What's up?
I'm wondering why it seems that adding a method to the prototype of a string literal seems to work, but adding a property does not? I was playing with ideas in relation to this question, and have the following code:
String.prototype._str_index1 = 0;
String.prototype._str_reset = function() {
this._str_index1 = 0;
};
String.prototype._str_substr = function(len) {
var ret = this.substr(this._str_index1, len);
this._str_index1 = this._str_index1 + len;
return ret;
};
var testString = new String('Loremipsumdolorsitamet,consectetur');
log(testString._str_substr(5));
log(testString._str_substr(4));
This works fine. If however I change the third-last line to:
var testString = 'Loremipsumdolorsitamet,consectetur';
...it seems that although the method _str_substr
exists and is callable on the string literal, the value of the property _str_index1
is always 0.
What's up?
Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Sep 21, 2010 at 0:43 sje397sje397 41.9k9 gold badges89 silver badges106 bronze badges 2- Most of the things you call ‘string literal’, are actually called ‘string primitive’: literals are a syntax to describe a string, e.g., to assign a string (or number, boolean, object, etc.) to a variable; primitive values are non-posite values that variables and objects can contain (apart from (other) objects). – Marcel Korpel Commented Sep 21, 2010 at 1:23
- Except when it's not....'indexOf' is not a function when called against a string.... – Cris Stringfellow Commented Nov 3, 2012 at 7:48
2 Answers
Reset to default 9The string primitive is converted to a transient String
object every time you try to invoke a method of the String
object (the JavaScript engine internally converts a string primitive to a String
object when necessary). After this function returns, the String
object is (unobtrusively) converted back to a string primitive (under the hood) and this new primitive is returned (and most of the time assigned to a variable); every time a method of the String
object is invoked.
So, after each invocation of testString._str_substr
, _str_index1
is thrown away with the object and a new object (with a reset _str_index1
) is created when _str_substr
is called again.
See also MDC:
Because JavaScript automatically converts between string primitives and String objects, you can call any of the methods of the String object on a string primitive. JavaScript automatically converts the string primitive to a temporary String object, calls the method, then discards the temporary String object.
This happens because the object is created and immediately thrown away when the assignment is made, because it's a string literal.
So with the first version, an object is created and kept, so testString is an object, not a string literal. In the second case, an object is created and thrown away, so all properties get lost...
Now try replacing that line with this:
var testString = 'Loremipsumdolorsitamet,consectetur'._str_substr();
Interesting, right? It still returns a string primitive, but that could be fixed...
String.prototype._str_substr = function(len) {
var ret = this.substr(this._str_index1, len);
this._str_index1 = this._str_index1 + len;
return new String(ret);
};
Of course these are just suggestions designed to help explain why literals act differently than objects, not real-world remendations...
本文标签: javascriptString object versus literalmodifying the prototypeStack Overflow
版权声明:本文标题:javascript - String object versus literal - modifying the prototype? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742229888a2437026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论