简体   繁体   English

为什么将“ .keys”应用于Javascript中的String对象时返回“ undefined”?

[英]Why does '.keys' return 'undefined' when applied to the String object in Javascript?

I am trying to understand why the 'keys' method does not return the properties and methods of the String object. 我试图理解为什么'keys'方法不返回String对象的属性和方法。 In other words what is unique about this object? 换句话说,该对象的独特之处是什么? I tested this theory by creating a generic object, giving it 1 property and 1 method and then running the .keys method on it, and it returned both the property and the method. 我通过创建一个通用对象,为其提供了1个属性和1个方法,然后在其上运行.keys方法,对该理论进行了测试,它返回了该属性和该方法。 Since String is an object in Javascript, assumed applying .keys method to it would do the same —at the least returning the .length method in the returned set. 由于String是Javascript中的对象,因此假设对它应用.keys方法将执行相同的操作—至少在返回的集合中返回.length方法。

Using Chrome's console I ran the following cases: 我使用Chrome的控制台运行以下情况:

typeof String // "function"
"function" == typeof String // true
"object" == typeof String // false

Two notes in addition to my main question: In the scope of JavaScript: 除了我的主要问题外,还有两个注意事项:在JavaScript的范围内:

  1. Is a function not an object? 函数不是对象吗?
  2. Aren't most things objects outside primitives and some other special cases? 多数事物不是在原始类型和某些其他特殊情况下成为对象吗?

Functions are objects. 功能是对象。 The defined behavior of typeof is somewhat idiosyncratic. typeof的定义行为有些特质。 Values in JavaScript are either objects or primitives. JavaScript中的值可以是对象,也可以是基元。

The "keys" property of String instances is undefined because, well, it is not defined on the String prototype. 未定义String实例的“ keys”属性,因为在String原型上未定义它。 You can add such a property if you like, though working with String instances instead of string primitives is a recipe for lots of weird bugs. 您可以根据需要添加这样的属性,尽管使用String实例而不是字符串基元可以解决许多奇怪的错误。

console.log(String)

>function String() { [native code] }

var a = new String()
console.log(a)

>String {length: 0, [[PrimitiveValue]]: ""}

typeof a

>"object"

it should be "class" instead of "function", as like in other languages, but function in js are first-class object (more details here What is meant by 'first class object'? ) 与其他语言一样,它应该是“类”而不是“函数”,但是js中的函数是第一类对象(此处有更多详细信息,“第一类对象”是什么意思?

object are the instanced classes 对象是实例化的类

Some background information 一些背景资料

Effectively Object.keys returns a list of own enumerable properties on the Object instance. 实际上, Object.keys返回Object实例上自己的可枚举属性的列表。 These are properties belonging only to that instance of the Object class, excluding any prototypically inherited properties and or methods. 这些属性仅属于Object类的实例,不包括任何原型继承的属性和/或方法。 These are values that would return true in the following examples: 这些值在以下示例中将返回true:

// Create an Object with own properties
var obj = {
  foo: 'bar'
}

obj.hasOwnProperty('foo') // => true, brilliant

As many will know, this would still hold true when properties are added after the Object has been constructed. 众所周知,在构造对象之后添加属性时,这仍然适用。 As can be seen in this example: 如本例所示:

// Create an Object the retroactively add an enumerable property
var obj = {}
obj.foo = 'bar'

obj.hasOwnProperty('foo') // => true, great

Objects, functions, and arrays all behave this way; 对象,函数和数组都以这种方式运行。 whereas strings don't. 而字符串则没有。 You can easily see this by trying to add own properties to a string and then reading them back, like this: 您可以尝试将自己的属性添加到字符串中,然后再读回它们,从而很容易地看到这一点,如下所示:

var str = 'foo'
str.bar = 'baz'

console.log(str)  // => undefined, hmm

// What about "hasOwnProperty"?
str.hasOwnProperty('bar') // => false... too bad

So to answer the question... 所以要回答这个问题...

Own enumerable properties inherently cannot exist on instances of the String type and cannot be added retrospectively either, because own properties are not assignable to strings, period. 自己的可枚举属性固有地不能存在于String类型的实例上,也不能追溯地添加,因为自己的属性无法分配给字符串period。

Although this explanation doesn't explain the decisions made whilst implementing this, it certainly gives some underlying sanity as to why String.keys doesn't, or simply can't, exist; 尽管这种解释并没有解释在实现过程中所做的决定,但是对于String.keys为什么不存在或根本就不存在,它肯定给出了一些基本的理智。 and why Object.keys always returns undefined when supplied with a string. 以及为什么在提供字符串时Object.keys总是返回undefined。

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

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