当我使用一个变量进行比较时,低于这个值的Javascript会失败
当我将比较的右侧从Math.PI替换为我的变量this.bottomchainangelrads时,我有两个“if-less-then”块,它们似乎对我不起作用 上下文:我正在为两个齿轮之间的链设置动画,因此在两个齿轮的轮齿上迭代以隐藏/显示其旋转时的链接 在前面的代码中,变量是用数学而不是字符串初始化的当我使用一个变量进行比较时,低于这个值的Javascript会失败,javascript,variables,comparison-operators,Javascript,Variables,Comparison Operators,当我将比较的右侧从Math.PI替换为我的变量this.bottomchainangelrads时,我有两个“if-less-then”块,它们似乎对我不起作用 上下文:我正在为两个齿轮之间的链设置动画,因此在两个齿轮的轮齿上迭代以隐藏/显示其旋转时的链接 在前面的代码中,变量是用数学而不是字符串初始化的 this.bottomChainAngleRads = Math.PI + 2 * Math.atan2(...); 然后我想每隔一段时间用它做点什么: this.step = functi
this.bottomChainAngleRads = Math.PI + 2 * Math.atan2(...);
然后我想每隔一段时间用它做点什么:
this.step = function() {
console.log('this.bottomChainAngleRads = ' + this.bottomChainAngleRads // Just over PI. Usually about 3.4.
+ ' ' + $.isNumeric(this.bottomChainAngleRads)); // Always true.
// Counting the passing through each if block. Expecting a little in each.
var frontDisplay = 0, frontHide = 0, rearDisplay = 0, rearHide = 0;
$(this.frontGear.div).find('.geartooth').each(function(index, el) {
var totalRadians = measureRotation(el);
console.log('front totalRadians = ' + totalRadians + ' ' // From 0 to TWO_PI
+ (totalRadians < this.bottomChainAngleRads)); // Always false. WTF.
if (totalRadians < this.bottomChainAngleRads) { // <================ FAILS. NEVER TRUE.
// if (totalRadians < Math.PI) { // MOSTLY CORRECT, but expectedly off by minor angle.
++frontDisplay;
// .. do stuff
} else {
++frontHide;
// .. do other stuff
}
});
$(this.rearGear.div).find('.geartooth').each(function(index, el) {
var totalRadians = measureRotation(el);
console.log('rear totalRadians = ' + totalRadians + ' ' // From 0 to TWO_PI
+ (totalRadians < this.bottomChainAngleRads)); // Always false. WTF.
if (totalRadians < this.bottomChainAngleRads) { // <================ FAILS. NEVER TRUE.
// if (totalRadians < Math.PI) { // MOSTLY CORRECT, but expectedly off by minor angle.
++rearHide;
// .. do stuff
} else {
++rearDisplay;
// .. do other stuff
}
});
// Below I expected approximately a 50/50 split on each gear. Instead, I get...
console.log('front: ' + frontDisplay + ', ' + frontHide // Nothing, All.
+ '; rear: ' + rearDisplay + ', ' + rearHide); // All, Nothing
}
this.step=function(){
log('this.bottomChainAngleRads='+this.bottomChainAngleRads//略高于PI。通常约为3.4。
+''+$.isNumeric(this.bottomchainangelrads));//始终为真。
//计算通过每个if块的次数。在每个if块中期待一点。
var frontDisplay=0,frontHide=0,realldisplay=0,reallhide=0;
$(this.frontGear.div).查找('.geartooth')。每个(函数(索引,el){
var totalRadians=测量值(el);
log('front totalRadians='+totalRadians+'.//从0到2
+(totalRadians 如果(totalRadians
回调中的上下文不再是您的对象实例,此
指向单个DOM元素(.geartooth
),这些元素显然没有属性bottomChainAngleRads
最简单的修复方法是保存正确的上下文引用。请尝试以下操作:
var self = this;
$(this.rearGear.div).find('.geartooth').each(function(index, el) {
var totalRadians = measureRotation(el);
if (totalRadians < self.bottomChainAngleRads) {
...
});
var self=this;
$(this.rearGear.div).查找('.geartooth')。每个(函数(索引,el){
var totalRadians=测量值(el);
if(总弧度<自底链角度){
...
});
这是一个范围问题。这个在内部。每个引用一个DOM元素,而不是您的对象。在外部使用var that=This
。每个然后在适当的地方使用that
谢谢。我现在可以停止头痛了。您还可以使用jQuery设置上下文。每个($.proxy)(myFunction,this))
。这对于事件处理程序尤其有用,因为在事件处理程序中,可以方便地使用代理也可以处理的部分应用程序。