规范问题如果在用箭头函数替换函数声明/表达式后发现有关问题的问题,请将其作为此副本的副本关闭.
ES2015中的箭头功能提供了更简洁的语法.我现在可以用箭头功能替换所有函数声明/表达式吗?我需要注意什么?
例子:
构造函数
function User(name) {
this.name = name;
}
// vs
const User = name => {
this.name = name;
};
Run Code Online (Sandbox Code Playgroud)
原型方法
User.prototype.getName = function() {
return this.name;
};
// vs
User.prototype.getName = () => this.name;
Run Code Online (Sandbox Code Playgroud)
对象(文字)方法
const obj = {
getName: function() {
// ...
}
};
// vs
const obj = {
getName: () => {
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
回调
setTimeout(function() {
// ...
}, 500);
// vs
setTimeout(() => {
// ...
}, …Run Code Online (Sandbox Code Playgroud) 我知道>=运算符意味着大于或等于,但我=>在一些源代码中看到过.那个运营商的意义是什么?
这是代码:
promiseTargetFile(fpParams, aSkipPrompt, relatedURI).then(aDialogAccepted => {
if (!aDialogAccepted)
return;
saveAsType = fpParams.saveAsType;
file = fpParams.file;
continueSave();
}).then(null, Components.utils.reportError);
Run Code Online (Sandbox Code Playgroud) 这是代码:
function accum(s) {
return s.split('').map((x,index) => x.toUpperCase()+Array(index+1).join(x.toLowerCase())).join('-');
}
Run Code Online (Sandbox Code Playgroud)
我想知道"=>"是什么.此函数接受一个字符串,并且对于每个元素的索引号,它将许多元素添加到输出中.这是一个例子:
accum("abcd") --> "A-Bb-Ccc-Dddd"
accum("RqaEzty") --> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") --> "C-Ww-Aaa-Tttt"
Run Code Online (Sandbox Code Playgroud) 我正在使用FileReader接口和它的异步方法readAsText()来读取本地文本文件,之后当调用onload事件时,我尝试读取我的文件,我的源代码如下所示:
export class ReadFileComponent {
text: string;
readFile(): void {
let reader=new FileReader();
reader.onload = function(e) {
this.text=reader.result;
}
reader.readAsText(file);
}
}
Run Code Online (Sandbox Code Playgroud)
编译失败,因为"FileReader"类型上不存在属性"text "
我认为这是由于EventListener接口不接受对象,
有人请解决这类问题吗?
多谢你们,
我总是看到用箭头函数语法定义的纯React组件的示例:
const foo = () => (...);
export default foo;
Run Code Online (Sandbox Code Playgroud)
而不是更传统的函数定义语法:
function foo() {
return ...;
}
export default foo;
Run Code Online (Sandbox Code Playgroud)
是否有理由更喜欢前者而不是后者?
我有一个非常简单的示例,其中TypeScript(3.5.1)可以很好地使用代码,但运行时会立即引发错误。
我相信这里的问题是本质上value是在运行之前声明但未初始化的getValue。这是非常不直观的imo,但我知道这是JS的工作方式。
但是,为什么在这样一个简单的示例中TS无法检测到此问题?由于value是const,在我看来TS应该能够准确确定设置的时间并预测此代码将崩溃。
console.log(getValue());
const value = "some string";
function getValue() {
return value;
}
Run Code Online (Sandbox Code Playgroud)
在没有函数调用的第二个示例中,TS确实发现在赋值之前使用了变量:
console.log(value);
const value = "some string";
Run Code Online (Sandbox Code Playgroud)
TSLint的声明前无用似乎也不适用。
假设TS /棉绒将无法捕捉到这一点,那么在初始示例中是否有最佳实践来避免这种崩溃?例如,“始终在文件顶部声明模块级const”。
以不同的方式在ES6/ES2015中创建顶级功能有哪些优点/缺点?或者这仅仅是品味/风格指南等问题?
选项1:
function square(n) {
return n * n;
}
Run Code Online (Sandbox Code Playgroud)
选项2:
var square = function(n) {
return n * n;
};
Run Code Online (Sandbox Code Playgroud)
选项3:
var square = (n) => {
return n * n;
};
Run Code Online (Sandbox Code Playgroud)
选项4:
const square = (n) => {
return n * n;
};
Run Code Online (Sandbox Code Playgroud) 据我所知,箭头功能类似于普通功能.我这样使用时没有问题:
let X = () => {};
let Y = function() {};
X();
Y();Run Code Online (Sandbox Code Playgroud)
但是,当我使用它时发生错误 new
let X = () => {};
let Y = function() {};
x = new X();
y = new Y();Run Code Online (Sandbox Code Playgroud)
Uncaught TypeError: X is not a constructor
你能解释一下为什么吗?非常感谢.
我有一个方法handleError(),如文档https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling
private handleError(error: any) {
console.error(error);
console.log(this.loginService); // <- always undefined
return Observable.throw(error);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,this.loginService是未定义的,尽管它已在我的类中正确注入.它已经在其他方法中使用,但似乎在handleError中不可用.
http-catch调用方法的方式可能是问题吗?如果是这样,我怎么能绕过那个?处理错误时我需要执行一些逻辑.
这是一个关于如何将handleError方法设置为回调的示例(与文档完全一样)
this.http.get(url,
ApiRequest.ACCEPT_JSON)
.map(ApiHelper.extractData)
.catch(this.handleError);
Run Code Online (Sandbox Code Playgroud) javascript ×8
ecmascript-6 ×6
angular ×2
function ×2
typescript ×2
filereader ×1
reactjs ×1
return ×1
syntax ×1