다음과 같은 주요 기능으로 인해 'setState'는 유효한 속성 예외가 아닙니다.
componentWillMount = function () {
api.daysOfMonth(this, this.props.month, this.props.year, function (ds) {
var days = [];
ds.forEach(function (jsonDay) {
var day = {date: jsonDay.date, inRange: jsonDay.inRange};
days.push(day);
});
this.setState({ daysOfMonth: days });
});
api.js의 스 니펫은 다음과 같습니다. 주 함수 호출 Api.daysOfMonth (...), Api.daysOfMonth는 전역 객체를 사용하여 ajax 메서드를 호출하고 프로토콜 적용을 사용하여 콜백 함수를 호출합니다. 콜백 함수는 위 스크립트와 같이 주 함수에서 전달됩니다.
ApiImpl = (function () {
function ApiImpl() { }
ApiImpl.prototype.invoke = function (callerObj, callback, action) {
var params = [];
for (var _i = 3; _i < arguments.length; _i++) {
params[_i - 3] = arguments[_i];
}
params.push(callback); //push callback function into params, so object of QWebChannel can callback the function after execute the 'action'
if (typeof window['api'] === 'undefined') {
new QWebChannel(qt.webChannelTransport, function (channel) {
window['api'] = channel.objects.api;
var func = window['api'][action].bind(callerObj);
return func.apply(callerObj, params); //here goes error
});
}
var func = window['api'][action].bind(callerObj);
return func.apply(callerObj, params); //here goes error
};
return ApiImpl;
}());
Api = (function () {
function Api() {
}
Api.daysOfMonth = function (callerObj, month, year, callback) {
this.impl.invoke(callerObj, callback, 'daysOfMonth', month, year);
};
return Api;
}());
Api.impl = new ApiImpl();
exports_1("Api", Api);
이것이 귀하의 질문에 대답한다면, 그 이유는 새로 생성 된 함수를 첫 번째 인수로 전달 한 함수에 .bind()
바인딩 하기 때문 this
입니다.
componentWillMount = function () {
api.daysOfMonth(this, this.props.month, this.props.year, function (ds) {
var days = [];
ds.forEach(function (jsonDay) {
var day = {date: jsonDay.date, inRange: jsonDay.inRange};
days.push(day);
});
this.setState({ daysOfMonth: days });
}.bind(this));
참조 : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
es6를 작성하면 화살표 기능을 사용하여 동일한 작업을 수행 할 수 있습니다.
componentWillMount = function () {
api.daysOfMonth(this, this.props.month, this.props.year, (ds) => {
var days = [];
ds.forEach(function (jsonDay) {
var day = {date: jsonDay.date, inRange: jsonDay.inRange};
days.push(day);
});
this.setState({ daysOfMonth: days });
});
요컨대, this
화살표 함수는 화살표 함수가 정의 된 곳 this
과 동일 하다는 것을 의미하는 어휘 범위입니다 this
.
참조 : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다