简体   繁体   English

为什么String.prototype中的“this”会返回一个对象?

[英]Why does “this” in String.prototype return an object?

This might be a duplicate, dumb or off-topic but the actual question is the question's title. 这可能是重复的,愚蠢的或偏离主题的,但实际的问题是问题的标题。

I wanted to make something simple as this: 我想做一些简单的事情:

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

And when invoked (in Firebug for instance), the result is: 并且在调用时(例如在Firebug中),结果是:

String { 0="t",  1="e",  2="s",  more...}

For: "test".log() and of course the same thing is returned. 对于: "test".log() ,当然返回相同的东西。

Now, the workaround for this was either a "" concatenation on any side or this.toString() . 现在,解决方法是在任何一方使用""连接或this.toString()

Why is this an object in the String.prototype scope and the instanceof is both of Object and String ? 为什么thisString.prototype范围内的对象, instanceofObjectString

When you create a string you actually create an instance (object) of the string function. 创建字符串时,实际上是创建字符串函数的实例(对象)。

Type String in the chrome console. 在chrome控制台中键入String

console.log(String);
Output : function String()

If you create a string 如果您创建一个字符串

var x = new String("aaa");

x is actually an instance of the String function x实际上是String函数的一个实例

console.log(x instanceof String); // true

Why x is instance of Object? 为什么x是Object的实例?

In JavaScript every object has a __proto__ link. 在JavaScript中,每个对象都有一个__proto__链接。

x.__proto__ == String.prototype;

It means x 's proto link points to the String prototype. 这意味着x的proto链接指向String原型。

Now String.prototype has a __proto__ too. 现在String.prototype也有一个__proto__

String.prototype.__proto__ == Object.prototype;

instanceof operator first search for __proto__ link and follows it. instanceof运算符首先搜索__proto__链接并跟随它。

x 's proto link points to String 's prototype and String 's prototype points to Object prototype. x的原型链接指向String的原型, String的原型指向Object原型。

Hence x is an instance of Object too. 因此x也是Object一个实例。

Not sure but found something that might answer the question: 不确定,但找到了可能回答这个问题的东西:

String(Value) 字符串值)

When String is called with argument value, the following steps are taken: 使用参数值调用String ,将执行以下步骤:

If no arguments were passed to this function invocation, let s be "". 如果没有参数传递给这个函数调用,那么让s为“”。

Else, 其他,

If NewTarget is undefined and Type(value) is Symbol, return SymbolDescriptiveString(value). 如果未定义NewTarget且Type(value)为Symbol,则返回SymbolDescriptiveString(value)。

Let s be ToString(value). 我们是ToString(值)。

ReturnIfAbrupt(s). ReturnIfAbrupt(S)。

If NewTarget is undefined, return s. 如果未定义NewTarget,则返回s。

Return StringCreate(s, GetPrototypeFromConstructor(NewTarget, "%StringPrototype%")). 返回StringCreate(s,GetPrototypeFromConstructor(NewTarget,“%StringPrototype%”))。

The length property of the String function is 1. String函数的length属性为1。

Now StringCreate returns a String exotic object . 现在StringCreate返回一个String异域对象

According to it, 根据它,

A String object is an exotic object that encapsulates a String value and exposes virtual integer indexed data properties corresponding to the individual code unit elements of the String value. String对象是一个奇异的对象,它封装了一个String值,并公开了与String值的各个代码单元元素对应的虚拟整数索引数据属性。

Exotic String objects always have a data property named "length" whose value is the number of code unit elements in the encapsulated String value. Exotic String对象始终具有名为“length”的数据属性,其值是封装的String值中的代码单元元素的数量。 Both the code unit data properties and the "length" property are non-writable and non-configurable. 代码单元数据属性和“长度”属性都是不可写和不可配置的。

The best answer I can find, pertaining directly to this question, is answered in the ECMAScript specification on which JavaScript is based. 我可以找到的与此问题直接相关的最佳答案在JavaScript所基于的ECMAScript规范中得到了解答。

Here's the specification for String... http://www.ecma-international.org/ecma-262/5.1/#sec-8.4 这是String的规范...... http://www.ecma-international.org/ecma-262/5.1/#sec-8.4

An excerpt: 摘录:

The String type is the set of all finite ordered sequences of zero or more 16-bit unsigned integer values (“elements”). String类型是零个或多个16位无符号整数值(“元素”)的所有有限有序序列的集合。 The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a code unit value (see Clause 6). String类型通常用于表示正在运行的ECMAScript程序中的文本数据,在这种情况下,String中的每个元素都被视为代码单元值(参见条款6)。 Each element is regarded as occupying a position within the sequence. 每个元素被视为占据序列内的位置。 These positions are indexed with nonnegative integers. 这些位置用非负整数索引。 The first element (if any) is at position 0, the next element (if any) at position 1, and so on. 第一个元素(如果有)位于位置0,下一个元素(如果有)位于位置1,依此类推。 The length of a String is the number of elements (ie, 16-bit values) within it. String的长度是其中的元素数(即16位值)。 The empty String has length zero and therefore contains no elements. 空String的长度为零,因此不包含任何元素。

Especially this part: 特别是这部分:

Each element is regarded as occupying a position within the sequence. 每个元素被视为占据序列内的位置。 These positions are indexed with nonnegative integers. 这些位置用非负整数索引。

This explains why the actual truer storage of the String being revealed is array-like in nature. 这就解释了为什么显示的String的实际更真实的存储本质上是类似于数组的。 The characters are INDEXED sequentially as is mandated within the specification. 按照规范中的要求,字符按顺序索引。

原始字符串“测试”的盒装入类型的对象String的前log被调用的功能。

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

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