好的,我的问题很简单:在 JavaScript / ES6
当你有类似的事情时会发生什么
x = 5;
console.log(x); // 5
Run Code Online (Sandbox Code Playgroud)
解释器是在运行时自动添加“let”还是为什么它可以正常工作而没有错误?
编辑:严格模式 ES5 的语法允许称为隐式全局变量的东西,这是许多令人沮丧的编程错误的根源。简而言之,如果你忘记用 var 声明一个变量,JavaScript 会很高兴地假设你指的是一个全局变量。如果不存在这样的全局变量,它会创建一个!你可以想象这造成的问题。
我知道了。感谢您的所有评论。我现在明白为什么会这样了。谢谢!
我想知道-类方法,作为函数的类属性和作为箭头函数的类属性之间有什么区别?在不同的方法变体中,“ this”关键字的行为是否有所不同?
class Greeter {
constructor() {
this.greet();
this.greet2();
this.greet3();
}
greet() {
console.log('greet1', this);
}
greet2 = () => {
console.log('greet2', this);
}
greet3 = function() {
console.log('greet3', this);
}
}
let bla = new Greeter();
Run Code Online (Sandbox Code Playgroud)
编辑:从编译的打字稿的javascript输出:
var Greeter = /** @class */ (function () {
function Greeter() {
var _this = this;
this.greet2 = function () {
console.log('greet2', _this);
};
this.greet3 = function () {
console.log('greet3', this);
};
this.greet();
this.greet2();
this.greet3();
}
Greeter.prototype.greet = function () {
console.log('greet1', …
Run Code Online (Sandbox Code Playgroud)