无法读取未定义的属性“草”

Posted

技术标签:

【中文标题】无法读取未定义的属性“草”【英文标题】:cannot read property 'grass' of undefined 【发布时间】:2021-09-23 13:22:11 【问题描述】:

目标:长数组将包含this.token.grass 的值。

module.exports = 
    render: function() 

    ,
    keyDown: function(k) 

    ,
    level: [
        [this.token./** Error Here **/grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass],
        [this.token.grass, this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass,this.token.grass]
    ],
    NonCollidable: "$$$NonCollidable\\Main",
    Collidable: "$$$Colldiable\\Main",
    token: 
        grass: RenderToken: "\x1b[32m#\x1b[89m\x1b[0m", TokenType: this.NonCollidable
    

错误:

TypeError: Cannot read property 'grass' of undefined
    at Object.<anonymous> (C:\Users\#\Desktop\libraries\unnamed\main.js:10:21)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.pointer (C:\Users\#\Desktop\libraries\unnamed\context.js:4:76)
    at ReadStream.<anonymous> (C:\Users\#\Desktop\libraries\unnamed\io.js:13:25)
    at ReadStream.emit (events.js:315:20)

老实说,我什么都没试过。我以为this 在那种情况下可能是指父数组,但我也不知道如何解决。

代码如上。

【问题讨论】:

【参考方案1】:

level 设为一个函数并使其返回一个数组。注意不要使用箭头函数,因为this 在箭头函数内部会有所不同

module.exports = 
  render: function() ,
  keyDown: function(k) ,
  token: 
    grass: 
      RenderToken: '\x1b[32m#\x1b[89m\x1b[0m',
      TokenType: this.NonCollidable
    
  ,
  level: function() 
    return [
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ],
      [
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass,
        this.token.grass
      ]
    ];
  ,
  NonCollidable: '$$$NonCollidable\\Main',
  Collidable: '$$$Colldiable\\Main'
;

【讨论】:

这行得通。我认为这里所有的数组元素都会指向同一个对象,只是指出来。 @Obcuredツ【参考方案2】:

this 在您的上下文中将引用全局对象,在大多数情况下是 window 对象。看看:

https://developer.mozilla.org/en-US/docs/Web/javascript/Reference/Operators/this

【讨论】:

以上是关于无法读取未定义的属性“草”的主要内容,如果未能解决你的问题,请参考以下文章

带有 Ionic 4 的 SQLite?无法读取未定义类型错误的属性“then”:无法读取未定义的属性“then”

`无法读取未定义的属性(读取'组件')`

未捕获的类型错误:无法读取未定义的属性“区域”?

NextJS:未捕获的类型错误:无法读取未定义的属性(读取“属性”)

未捕获的类型错误:无法读取未定义的属性 toLowerCase

使用地图时反应'无法读取未定义的属性'