控制台日志记录 "this" returns "null"
Console logging "this" returns "null"
我正在尝试为我正在构建的 React 应用程序创建一个 flux 存储。我正在使用对象分配 polyfill npm 包和 Facebook 的 Flux 库。
最初我收到错误 "Cannot read property '_data' of null' error in the console which was refering to var currIds = this._data.map(function(m){return m.id;});
. That method is currently the only one being called directly. I then did console.log(this)
which returned "null"。
我觉得这很奇怪。怎么回事?
我的代码:
var Assign = require('object-assign');
var EventEmitterProto = require('events').EventEmitter.prototype;
var CHANGE_EVENT = 'CHANGE';
var StoreMethods = {
init: function() {},
set: function (arr) {
console.log(this);
var currIds = this._data.map(function(m){return m.id;});
arr.filter(function (item){
return currIds.indexOf(item.id) === -1;
}).forEach(this.add.bind(this));
},
add: function(item){
console.log(this);
this._data.push(item);
},
all: function() {
return this._data;
},
get: function(id){
return this._data.filter(function(item){
return item.cid === id;
})[0];
},
addChangeListener: function(fn) {
this.on(CHANGE_EVENT, fn);
},
removeChangeListener: function(fn) {
this.removeListener(CHANGE_EVENT, fn);
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
bind: function(actionType, actionFn) {
if(this.actions[actionType]){
this.actions[actionType].push(actionFn);
} else {
this.actions[actionType] = [actionFn];
}
}
};
exports.extend = function(methods) {
var store = {
_data: [],
actions: {}
};
Assign(store, EventEmitterProto, StoreMethods, methods);
store.init();
require('../dispatcher').register(function(action){
if(store.actions[action.actionType]){
store.actions[action.actionType].forEach(function(fn){
fn.call(null, action.data);
})
}
});
return store;
};
我不能 see
在调用 set 的地方,但是你的 this
可以是 null
如果函数是通过 call
调用的(见 here) 或 apply
,您的第一个参数是 null
。
这也发生在您的 require.register
回调中:
fn.call(null, action.data) //First parameter is your 'this'.
我正在尝试为我正在构建的 React 应用程序创建一个 flux 存储。我正在使用对象分配 polyfill npm 包和 Facebook 的 Flux 库。
最初我收到错误 "Cannot read property '_data' of null' error in the console which was refering to var currIds = this._data.map(function(m){return m.id;});
. That method is currently the only one being called directly. I then did console.log(this)
which returned "null"。
我觉得这很奇怪。怎么回事?
我的代码:
var Assign = require('object-assign');
var EventEmitterProto = require('events').EventEmitter.prototype;
var CHANGE_EVENT = 'CHANGE';
var StoreMethods = {
init: function() {},
set: function (arr) {
console.log(this);
var currIds = this._data.map(function(m){return m.id;});
arr.filter(function (item){
return currIds.indexOf(item.id) === -1;
}).forEach(this.add.bind(this));
},
add: function(item){
console.log(this);
this._data.push(item);
},
all: function() {
return this._data;
},
get: function(id){
return this._data.filter(function(item){
return item.cid === id;
})[0];
},
addChangeListener: function(fn) {
this.on(CHANGE_EVENT, fn);
},
removeChangeListener: function(fn) {
this.removeListener(CHANGE_EVENT, fn);
},
emitChange: function() {
this.emit(CHANGE_EVENT);
},
bind: function(actionType, actionFn) {
if(this.actions[actionType]){
this.actions[actionType].push(actionFn);
} else {
this.actions[actionType] = [actionFn];
}
}
};
exports.extend = function(methods) {
var store = {
_data: [],
actions: {}
};
Assign(store, EventEmitterProto, StoreMethods, methods);
store.init();
require('../dispatcher').register(function(action){
if(store.actions[action.actionType]){
store.actions[action.actionType].forEach(function(fn){
fn.call(null, action.data);
})
}
});
return store;
};
我不能 see
在调用 set 的地方,但是你的 this
可以是 null
如果函数是通过 call
调用的(见 here) 或 apply
,您的第一个参数是 null
。
这也发生在您的 require.register
回调中:
fn.call(null, action.data) //First parameter is your 'this'.