0

I am trying to understand why the 'keys' method does not return the properties and methods of the String object. 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. 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.

Using Chrome's console I ran the following cases:

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

Two notes in addition to my main question: In the scope of JavaScript:

  1. Is a function not an object?
  2. Aren't most things objects outside primitives and some other special cases?
brooklynsweb
  • 817
  • 3
  • 12
  • 26
  • 4
    It's not clear exactly how the question title relates to the question content. – Pointy Feb 16 '17 at 16:33
  • You can argue that any primitive has the appearance of an Object when inspected. All have inherent properties and can be treated as though they were Objects themselves. The question is pretty academic though IMO. – Iain J. Reid Feb 16 '17 at 16:38
  • @Iain, from what I read on MDN, Javacsript automatically converts string primitives to String objects. But in my example I was testing the String object itself. – brooklynsweb Feb 16 '17 at 17:06
  • @Pointy, fair point, updated the the question content. – brooklynsweb Feb 16 '17 at 17:12
  • @brooklynsweb hopefully my answer can provide some clarity on why strings behave in this way. I wish though that I could have provided some deeper information as to why this behaviour was chosen though. – Iain J. Reid Feb 16 '17 at 17:35
  • @brooklynsweb did my answer help at all? Would be good to hear some feedback, it's pretty low-level so there aren't many resources out there to get much closure on this! – Iain J. Reid Mar 01 '17 at 10:06

3 Answers3

0

Functions are objects. The defined behavior of typeof is somewhat idiosyncratic. Values in JavaScript are either objects or primitives.

The "keys" property of String instances is undefined because, well, it is not defined on the String prototype. 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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0
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'?)

object are the instanced classes

Community
  • 1
  • 1
Simone Sanfratello
  • 1,520
  • 1
  • 10
  • 21
0

Some background information

Effectively Object.keys returns a list of own enumerable properties on the Object instance. These are properties belonging only to that instance of the Object class, excluding any prototypically inherited properties and or methods. These are values that would return true in the following examples:

// 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.

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; and why Object.keys always returns undefined when supplied with a string.

Iain J. Reid
  • 978
  • 1
  • 11
  • 23