简体   繁体   English

字符串对象与文字 - 修改原型?

[英]String object versus literal - modifying the prototype?

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. ...似乎虽然方法_str_substr存在并且可以在字符串文字上调用,但属性_str_index1值始终为0。

What's up? 这是怎么回事?

The 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). 每次尝试调用String对象的方法时,字符串基元都会转换为瞬态String对象(JavaScript引擎会在必要时在内部将字符串基元转换为String对象)。 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); 在此函数返回后, String对象被(不引人注意地)转换回字符串原语(在引擎盖下)并返回此新原语(并且大部分时间分配给变量); every time a method of the String object is invoked . 每次调用String对象的方法时

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. 因此,在每次调用后testString._str_substr_str_index1扔掉与对象和新的对象(复位_str_index1时创建) _str_substr被再次调用。

See also MDC : 另见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自动在字符串基元和String对象之间进行转换,所以可以在字符串基元上调用String对象的任何方法。 JavaScript automatically converts the string primitive to a temporary String object, calls the method, then discards the temporary String object. JavaScript自动将字符串原语转换为临时String对象,调用该方法,然后丢弃临时String对象。

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. 因此,对于第一个版本,会创建并保留一个对象,因此testString是一个对象,而不是字符串文字。 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 recommendations... 当然,这些仅仅是为了帮助解释为什么文字的行为与对象不同,而不是现实世界的建议......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM