TypeError in Node REPL: Unable to read property '0' from undefined when I add Object.prototype property

When I added the property nameto Object.prototypeand the link Object.prototype, I get the following error:

TypeError: Cannot read property '0' of undefined"

But I can read Object.prototype.name. Is the property namesomething special for Object.prototype? Why does this error occur?

The code was run in Node v6.9.5 on Mac OS X. Does anyone know how to solve this?

$ node
> Object.prototype
{}
> Object.prototype.value = 'foo';
'foo'
> Object.prototype.name = 'bar';
'bar'
> Object.prototype
TypeError: Cannot read property '0' of undefined
> Object.prototype.name
'bar'
> Object.prototype.value
'foo'
> delete Object.prototype.name
true
> Object.prototype
{ value: 'foo' }
> Object.prototype.name = 'bar';
'bar'
> Object.prototype
TypeError: Cannot read property '0' of undefined
> delete Object.prototype.value;
true
> Object.prototype
TypeError: Cannot read property '0' of undefined
    at Object.stylizeWithColor [as stylize] (util.js:242:43)
    at formatProperty (util.js:814:18)
    at util.js:654:12
    at Array.map (native)
    at formatObject (util.js:653:15)
    at formatValue (util.js:592:16)
    at Object.inspect (util.js:186:10)
    at REPLServer.self.writer (repl.js:468:19)
    at finish (repl.js:593:38)
    at REPLServer.defaultEval (repl.js:385:5)
+4
source share
2 answers

After some checking, I found the culprit and yes, the problem is that it is "name"used in a special way.

JavaScript, Node REPL, .

, :

function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
  return keys.map(function(key) {
    return formatProperty(ctx, value, recurseTimes, visibleKeys, key, false);
  });
}

value Object.prototype, .. { name: 'bar' } keys - , .. ["name"].

"name" formatProperty, / formatValue . , , - "[32m'bar'[39m".

, . , , [Object], [Getter] .. :

ctx.stylize(name, 'name');

name "name", 'name' , , 'name' .

, stylize stylizeWithColor, ctx.colors .

stylizeWithColor .

function stylizeWithColor(str, styleType) {
  var style = inspect.styles[styleType];

  if (style) {
    return `\u001b[${inspect.colors[style][0]}m${str}` + // <- ERROR: `inspect.colors[style][0]` becomes `undefined[0]`
           `\u001b[${inspect.colors[style][1]}m`;
  } else {
    return str;
  }
}

?

, stylizeWithColor 'name' styleType, styleType :

boolean
date
null
number
regexp
special
string
symbol
undefined

'name' , .

inspect.styles, inspect.styles['name'] undefined ( ) if.

// ...

var style = inspect.styles[styleType]; // <-- this should return `undefined`

if (style) { // <-- this shouldn't happen

// ...

, inspect.styles POJO "name" Object.prototype, inspect.styles['name'] 'name' , [[Prototype]] "bar".

// ...

var style = inspect.styles[styleType]; // <-- this returns "bar"` because it found it up the [[Prototype]] chain

if (style) { // <-- this happens because "bar" is truthy

// ...

, inspect.colors["bar"][0], inspect.colors - POJO, . , "bar" .

, undefined[0], .

, name , inspect.colors, , name .

node color printing


: , , inspect.styles , .

+5

:

  Object.getOwnPropertyNames(Object);
-1

Source: https://habr.com/ru/post/1670702/


All Articles