简体   繁体   English

String.prototype的“this”不返回字符串?

[英]a String.prototype's “this” doesn't return a string?

What is going on here? 这里发生了什么? Just when I thought I knew JS inside and out, this gem comes up. 正当我认为我内外都知道JS时,这个宝石出现了。

String.prototype.doNothing = function() {
  return this;
};

alert(typeof 'foo'.doNothing()) // object
alert(typeof 'foo')             // string

http://jsfiddle.net/dJBmf/ http://jsfiddle.net/dJBmf/

This is breaking some things that expect a string, such as jQuery's .text(str) method. 这打破了一些期望字符串的东西,比如jQuery的.text(str)方法。

To make sure you're always getting a string, trying using this code: 要确保您始终获取字符串,请尝试使用以下代码:

String.prototype.doNothing = function() {
    return this.toString();
};

alert(typeof 'foo'.doNothing())
alert(typeof 'foo')

In your original code, this is being returned as the string object and not the actual string. 在原始代码中, this将作为字符串对象而不是实际字符串返回。

Here's a thorough overview of the this keyword. 以下是 this关键字的全面概述 Basically, JavaScript converts it into an object, if it wasn't one. 基本上,JavaScript将其转换为对象,如果不是一个对象。

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisValue, and a caller provided argumentsList: 当控件进入函数对象F中包含的函数代码的执行上下文,调用者提供thisValue,以及调用者提供的argumentsList时,执行以下步骤:

  1. If the function code is strict code, set the ThisBinding to thisValue. 如果函数代码是严格代码,请将ThisBinding设置为thisValue。
  2. Else if thisValue is null or undefined, set the ThisBinding to the global object. 否则,如果thisValue为null或未定义,则将ThisBinding设置为全局对象。
  3. Else if Type(thisValue) is not Object, set the ThisBinding to ToObject(thisValue). 否则,如果Type(thisValue)不是Object,则将ThisBinding设置为ToObject(thisValue)。
  4. Else set the ThisBinding to thisValue 否则将ThisBinding设置为thisValue

Same thing happens to Numbers and Booleans. Numbers和Booleans也是如此。 A similar DoNothing function would return a type of object. 类似的DoNothing函数将返回一种对象。

strict模式运行代码以获得预期结果!

It's again the difference between string literals and strings, i believe? 我相信这又是字符串文字和字符串之间的区别吗? I once had a question answered here in SO: Property value of a String object in JavaScript 我曾经在SO中回答了一个问题: JavaScript中String对象的属性值

You could've also used the constructor property: 你也可以使用constructor属性:

'foo'.constructor === String; //=>true
'foo'.doNothing().constructor === String; //=>true

See also this SO question and this jsFiddle 另见这个问题和这个jsFiddle

If String.prototype.doNothing() breaks stuff expecting a string value, I would use return String(this) or this.toString() ( this.valueOf() also works here) indeed. 如果String.prototype.doNothing()打破了期望字符串值的东西,我会使用return String(this)this.toString()this.valueOf()也可以在这里工作)。

尝试以下方法:

return this + '';

To get a better understanding of what's going on try using console logs, like so: 为了更好地理解正在发生的事情,请尝试使用控制台日志,如下所示:

String.prototype.doNothing = function() {
    console.log(this);
    return this;
};

console.log(typeof 'foo'.doNothing());
console.log(typeof 'foo');

These are the results I get in Firefox: 这些是我在Firefox中得到的结果:

foo { 0="f", 1="o", more...}
object
string

So it seems in the prototype a string is represented as an object/an array of characters (which does make sense.) 所以在原型中似乎字符串被表示为一个对象/一个字符数组(这确实有意义。)

Whether or not you should use toString (as suggested by McHerbie) or casting as a type String (as suggested by mellamokb) depends, in my opinion, on what you plan to do with this value. 在我看来,你是否应该使用toString(由McHerbie建议)或者作为类型字符串(由mellamokb建议),取决于你打算用这个值做什么。 I would personally lean toward casting it as a String. 我个人倾向于把它作为一个字符串。

function print() {
  let str = this;
  console.log(str);
}

String.prototype.print = print;

let a = "hello";
 a.print();//output:"hello"

Try return String(this); 尝试return String(this); instead of return this; 而不是return this;

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

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