我有这个ES6代码,在用Babel编译它到ES5后,this
内部.each
的回调就变成了undefined
.我该如何解决这个问题?
let mediaBoxes = $(".now-thumbnail");
let titles = [];
mediaBoxes.each(() => {
let obj = {
index: i,
title: $(this).find(".now-thumbnail-bottomtext").text().trim()
};
titles.push(obj);
});
Run Code Online (Sandbox Code Playgroud) 目前,为了使受控输入在Stateless React组件中工作,我将无状态组件包装在Sate完整组件中.
例如,
const InputComponent = (props) => {
return (
<input value={props.name} onChange={props.handleChange} />
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: 'Tekeste'
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
name: event.target.value
});
}
render() {
return (
<InputComponent name={this.state.name} handleChange={this.handleChange} />
);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道的是一些事情.
我正在尝试从 React Native 应用程序取回我的文件上传到 AWS S3 的进度报告。如您所见,我正在监听httpUploadProgress
事件并尝试打印进度。但是这个回调函数没有被调用。我在这里缺少什么?
AWS.config.update({
region: AWS_REGION,
accessKeyId: user.credentials.AccessKeyId,
secretAccessKey: user.credentials.SecretKey,
sessionToken: user.credentials.SessionToken
});
const imageArrayBuffer = decode(image.data);
const s3 = new AWS.S3();
const params = {
Bucket: BUCKET_NAME,
Key: photoKey,
Body: imageArrayBuffer,
ContentType: image.type
};
s3
.upload(params)
.on('httpUploadProgress', function(evt) {
console.log(
'Uploaded :: ' + parseInt(evt.loaded * 100 / evt.total) + '%'
);
})
.send(function(err, data) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud) javascript amazon-s3 amazon-web-services aws-sdk react-native
我以为我理解了this
箭头功能之间的关系,但是下面的代码片段让我质疑我的理解。
let person = {
name: 'Jim',
sayName: () => {
console.log(this.name);
}
};
person.sayName();
Run Code Online (Sandbox Code Playgroud)
我知道箭头功能捕获this
了封闭上下文的值。我原以为this
会是对象,但实际上是Window
。
有人可以帮我理解为什么会这样吗?
javascript ×4
ecmascript-6 ×2
amazon-s3 ×1
aws-sdk ×1
babeljs ×1
jquery ×1
react-native ×1
reactjs ×1