TypeScript 变量作用域
TypeScript var scoping
所以我最近开始使用 TypeScript,到目前为止我很享受它,但是有一个问题我就是无法理解为什么它不能正常工作。
"var" 关键字仍然可以自然地在 TypeScript 中访问,我知道它是一个函数范围,而不是 "let" 关键字的块范围。我有以下代码,但我不明白为什么它不起作用:
class Calendar {
month: number;
months: string[];
weekday: number;
day: number;
constructor() {
this.month = date.getMonth();
this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'];
this.weekday = date.getDay();
this.day = date.getDate();
}
nextMonth() {
var months = this.months;
var currentmonth: number = this.month;
el('.icono-caretRight').addEventListener('click', () => {
currentmonth = (currentmonth + 1) % months.length;
}, false);
month = currentmonth;
return month;
}
previousMonth() {
el('.icono-caretLeft').addEventListener('click', () => {
month--;
}, false);
}
}
let calendar = new Calendar();
let month = calendar.month;
let nextmonth = calendar.nextMonth();
console.log(nextmonth);
查看 nextMonth() 函数。我不知道为什么 "month" 变量没有增加。我到底做错了什么?
注意:我使用的是 JSPM + TypeScript(插件类型脚本)。
提前致谢!
the "var" keyword is still accessible in TypeScript naturally and I know that it's a function scope, versus block scope for the "let" keyword. I have the following code and I can't figure out why it's not working:
您对scoping
的理解是正确的。
以下代码有效:
class Calendar {
month: number = 0;
months: string[];
constructor() {
this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'];
}
nextMonth() {
var months = this.months;
var currentmonth: number = this.month;
currentmonth = (currentmonth + 1) % months.length;
month = currentmonth;
return month;
}
}
let calendar = new Calendar();
let month = calendar.month; // 0
let nextmonth = calendar.nextMonth();
console.log(nextmonth); // 1
我怀疑您对 month
与 this.month
和/或 addEventListener
与 实际调用函数 感到困惑。
更多
根据用户评论:
I understand that the this.month is 0 since well the date.getMonth() is actually 0. My problem in that is doesn't want to increment, probably I don't get the addEventListener part of the function or maybe like you said I'm confused, because it's suppose to increment on click correct? It has to return a new value everytime the user clicks right? Or... Not sure anymore though
密码
el('.icono-caretRight').addEventListener('click', () => {
currentmonth = (currentmonth + 1) % months.length;
}, false);
可能是您想在 构造函数 中执行的操作。您很可能想要:
el('.icono-caretRight').addEventListener('click', () => {
this.nextMonth();
}, false);
所以我最近开始使用 TypeScript,到目前为止我很享受它,但是有一个问题我就是无法理解为什么它不能正常工作。
"var" 关键字仍然可以自然地在 TypeScript 中访问,我知道它是一个函数范围,而不是 "let" 关键字的块范围。我有以下代码,但我不明白为什么它不起作用:
class Calendar {
month: number;
months: string[];
weekday: number;
day: number;
constructor() {
this.month = date.getMonth();
this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'];
this.weekday = date.getDay();
this.day = date.getDate();
}
nextMonth() {
var months = this.months;
var currentmonth: number = this.month;
el('.icono-caretRight').addEventListener('click', () => {
currentmonth = (currentmonth + 1) % months.length;
}, false);
month = currentmonth;
return month;
}
previousMonth() {
el('.icono-caretLeft').addEventListener('click', () => {
month--;
}, false);
}
}
let calendar = new Calendar();
let month = calendar.month;
let nextmonth = calendar.nextMonth();
console.log(nextmonth);
查看 nextMonth() 函数。我不知道为什么 "month" 变量没有增加。我到底做错了什么?
注意:我使用的是 JSPM + TypeScript(插件类型脚本)。
提前致谢!
the "var" keyword is still accessible in TypeScript naturally and I know that it's a function scope, versus block scope for the "let" keyword. I have the following code and I can't figure out why it's not working:
您对scoping
的理解是正确的。
以下代码有效:
class Calendar {
month: number = 0;
months: string[];
constructor() {
this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'November', 'December'];
}
nextMonth() {
var months = this.months;
var currentmonth: number = this.month;
currentmonth = (currentmonth + 1) % months.length;
month = currentmonth;
return month;
}
}
let calendar = new Calendar();
let month = calendar.month; // 0
let nextmonth = calendar.nextMonth();
console.log(nextmonth); // 1
我怀疑您对 month
与 this.month
和/或 addEventListener
与 实际调用函数 感到困惑。
更多
根据用户评论:
I understand that the this.month is 0 since well the date.getMonth() is actually 0. My problem in that is doesn't want to increment, probably I don't get the addEventListener part of the function or maybe like you said I'm confused, because it's suppose to increment on click correct? It has to return a new value everytime the user clicks right? Or... Not sure anymore though
密码
el('.icono-caretRight').addEventListener('click', () => {
currentmonth = (currentmonth + 1) % months.length;
}, false);
可能是您想在 构造函数 中执行的操作。您很可能想要:
el('.icono-caretRight').addEventListener('click', () => {
this.nextMonth();
}, false);