当我在 Visual Studio Code 中运行此 JavaScript 代码时,某些 console.log() 操作可以工作,但有些则不行

问题描述 投票:0回答:1

我正在为我正在学习的课程进行 JavaScript 对象练习,但是当我运行代码时,我没有得到预期的结果。为了清晰起见,我已将代码精简为最重要的部分。

const myObject = {
    _array: [ 
        {item: 'a', item2: 'b', item3: 'c'},
        {item: 'd', item2: 'e', item3: 'f'},
        {item: 'g', item2: 'h', item3: 'i'}
        ],

    get items () {
        return this._array;
    },

    addThing (newItem1, newItem2, newItem3) {
        let thing = {
            item1: newItem1,
            item2: newItem2,
            item3: newItem3
        };
        /* `this.players.push(player);` is adding a new player object to the `_players` array of the `team` object. It uses the `push()` method to add the `player` object to the array. */
        this._array.push(thing);
      }

}
console.log('hello before');
myObject.addThing('j', 'k', 'l');
console.log(myObject._array);
console.log('hello after');

从终端窗口运行此命令可以得到转储数组的预期结果...

...但是用 F5 或运行并调试运行它给了我这个

我想了解为什么这样运行时没有 _array 的输出。

javascript debugging javascript-objects console.log
1个回答
0
投票

更多的是评论而不是答案(我三天前才开始,所以无法添加评论): 使用 Vivaldi 浏览器并按 F5 按钮可以正确打印所有内容,如屏幕截图所示,显示结果并且没有错误。您使用的是哪种浏览器或环境?

© www.soinside.com 2019 - 2024. All rights reserved.