但是在高版本的node中不使用"use strict"也直接报错了
- User
但是在高版本的node中不使用"use strict"也直接报错了
Assistant
你说得对!这是一个很重要的观察。
在较新版本的 Node.js 中,即使没有显式声明
"use strict",某些情况下也会自动进入严格模式:1. ES6 模块自动严格模式
如果你的文件是
.mjs或者在package.json中设置了"type": "module",那么整个文件都会自动运行在严格模式下:// file.mjs 或者 package.json 中 "type": "module" function Person(name) { this.name = name; this.sayHi = function() { console.log(this.name); }; } const p = new Person("Alice"); const hi = p.sayHi; hi(); // TypeError: Cannot read property 'name' of undefined
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.