上调用方法我有一段代码写成这样:const对象
private get headers(): Headers {
const headers = new Headers();
headers.set('Authorization', `Bearer ${this.auth.tokenSnapshot}`);
return headers;
}
headers
被声明const
。 (这是Angular 4代码,因此Headers
来自here)。
在TypeScript中,是否可以对声明为const
的实例变量调用变异方法?
在C++中,除非Set
方法本身被声明为const
,否则这是不可能的,但我不知道TypeScript是否可以这样说。
只需阅读[documentation](https://developer.mozilla.org/en-US/docs/Web/ JavaScript/Reference/Statements/const),它说* const声明为一个值创建一个只读引用。这并不意味着它所拥有的价值是不可变的,只是变量标识符不能被重新分配。* – 2017-08-31 14:05:25