Javascript 如何访问每个块的下划线中的对象属性
我有一个构造函数,它用这个初始化了3个属性 然后我有了另一个方法,它在this.actors上循环,并使用其他两个属性执行一些操作。但是,在u3;中,这些属性中的每个块都是可访问的 为了简化this.actors,this.mainPoint和this.actorsDistance将在z.每个块中取消定义Javascript 如何访问每个块的下划线中的对象属性,javascript,underscore.js,Javascript,Underscore.js,我有一个构造函数,它用这个初始化了3个属性 然后我有了另一个方法,它在this.actors上循环,并使用其他两个属性执行一些操作。但是,在u3;中,这些属性中的每个块都是可访问的 为了简化this.actors,this.mainPoint和this.actorsDistance将在z.每个块中取消定义 function FindNearestPoint(actors, mainPoint) { //Assign the mainPoints co-ord to mainPoint t
function FindNearestPoint(actors, mainPoint) {
//Assign the mainPoints co-ord to mainPoint
this.mainPoint = actors[mainPoint];
this.actors = actors;
this.actorsDistance = {};
}
FindNearestPoint.prototype.getActorsDistance = function () {
var i = 0;
_.each(this.actors, function(el,key,list){
this.actorsDistance[_.keys(this.actors)[i]] = Math.abs(
( this.actors[key][0] - this.mainPoint[0] ) +
( this.actors[key][1] - this.mainPoint[1] )
);
i++
});
return this.actorsDistance;
};
这是因为
This
关键字在中,每个循环都指向This.actors
本身。但是,\uUeach()
的第三个参数是上下文参数,所以只需使用它即可。即:
_.each(this.actors, function(el,key,list){
this.actorsDistance[_.keys(list)[i]] = Math.abs(
( list[key][0] - this.mainPoint[0] ) +
( list[key][1] - this.mainPoint[1] )
);
i++
}, this);
事实上,这应该足够清楚。不要使用this.actors[key]
,而只使用el
——这也是因为回调函数内部与外部不同
this.mainPoint
也会有同样的问题,但无论如何都应该将它们缓存在单独的变量中。如果不需要,可以将this
作为回调上下文作为第三个参数传递给like@thefoureye
与其变异this.actorsDistance
,不如返回一个新对象:
FindNearestPoint.prototype.getActorsDistance = function() {
var mainX = this.mainPoint[0],
mainY = this.mainPoint[1];
return _.reduce(this.actors, function(m, el, key){
m[key] = Math.abs( el[0] - mainX + el[1] - mainY );
return m;
});
};
当控件到达迭代器函数的\时,对的引用会发生变化。这就是为什么这些变量是未定义的。为了防止这种情况发生,将对this的引用存储在另一个变量中,并在迭代器函数中使用它,如下所示
FindNearestPoint.prototype.getActorsDistance = function() {
var that = this; // Make `that` refer the current `this`
_.each(this.actors, function(el, key, list) {
that.actorsDistance[el] = Math.abs(
(that.actors[key][0] - that.mainPoint[0]) +
(that.actors[key][1] - that.mainPoint[1])
);
});
return this.actorsDistance;
};
FindNearestPoint.prototype.getActorsDistance = function() {
_.each(this.actors, function(el, key, list) {
this.actorsDistance[el] = Math.abs(
(this.actors[key][0] - this.mainPoint[0]) +
(this.actors[key][1] - this.mainPoint[1])
);
}, this); // Setting the current context to `this`
return this.actorsDistance;
};
解决此问题的另一种方法是在调用时设置上下文
FindNearestPoint.prototype.getActorsDistance = function() {
var that = this; // Make `that` refer the current `this`
_.each(this.actors, function(el, key, list) {
that.actorsDistance[el] = Math.abs(
(that.actors[key][0] - that.mainPoint[0]) +
(that.actors[key][1] - that.mainPoint[1])
);
});
return this.actorsDistance;
};
FindNearestPoint.prototype.getActorsDistance = function() {
_.each(this.actors, function(el, key, list) {
this.actorsDistance[el] = Math.abs(
(this.actors[key][0] - this.mainPoint[0]) +
(this.actors[key][1] - this.mainPoint[1])
);
}, this); // Setting the current context to `this`
return this.actorsDistance;
};
如何在FindNearestPoint的实例上调用getActorsDistance
,如nearestDelivery.getActorsDistance();但是我现在正在尝试调用构造函数中的,并传入我要引用的属性:)确定您没有忘记两个Math.sqrt()
s?四眼您就是那个人!谢谢,现在想想这是常识!:)