不使用[...].splice(index, 1, change)
和[...][index] = change
方法更改数组。有关详细信息,请参阅示例。
注意:我知道js,它会在除t === 1
之外的任何地方发生变化。
任何人都可以解释这种行为,这是一个已知的错误,还是有解决方案?
我想找到它的解决方案,因为我想创建' Interpolator for Tweening library'支持相对字符串(对象,数组,数字在以后支持,我将添加)。
// RegExp variables
var numRegExp =
/\s+|([A-Za-z?().,{}:""[\]#]+)|([-+/*%]+=)?([-+*/%]+)?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g;
// Helpers
function s2f(val) {
var floatedVal = parseFloat(val);
return typeof floatedVal === "number" && !isNaN(floatedVal) ? floatedVal : val;
}
var relativeModes = {
'+=': 1,
'-=': 1,
'*=': 2,
'/=': 3,
'%=': 4
};
function r2n(s, e) {
if (typeof e === 'number') {
return e - s;
} else {
var rv = relativeModes[e.substr(0, 2)],
v = e.substr(2);
if (rv === 1) {
var e2 = e[0] + v;
return (s + parseFloat(e2)) - s;
} else if (rv === 2) {
return (s * +v) - s;
} else if (rv === 3) {
return (s / +v) - s;
} else if (rv === 4) {
return (s * (+v / 100)) - s;
}
}
return e;
}
function s2n(str) {
return str.match(numRegExp).map(s2f);
}
// Splitted functions
var stringTween = function(s, e, d) {
d = d !== undefined ? d : 10000;
var sv = s2n(s);
var ev = s2n(e);
var rv = ev.map(function(e, i) {
return typeof e === 'string' && e.indexOf('=') === 1 ? e : null;
});
ev = ev.map(function(e, i) {
return rv[i] !== null ? r2n(sv[i], e) : typeof e === 'number' ? e - sv[i] : e;
});
return function(t) {
var str = '';
for (var i = 0, len = ev.length; i < len; i++) {
var a = sv[i],
b = ev[i],
r = rv[i];
str += typeof b === 'number' ? a + b * t : a;
if (t === 1 && r !== null) {
document.body.innerHTML += 'This script executes <code>t === 1</code>, relative value isn\'t null, but <code>sv</code> or <code>ev</code> not changes, this is BUG?' + '<br/>';
sv[i] += b;
ev[i] = r2n(sv[i], r);
}
}
return str;
}
}
// Test case
var st = performance.now(),
rep = 1;
requestAnimationFrame(function anim(time) {
var t = (time - st) / 2000;
t = t > 1 ? 1 : t;
document.body.innerHTML += stringTween('translate3d(10px, 20px, 30px)', 'translate3d(+=20px, 30px, 35px)')(t) + '<br/>';
if (t === 1) {
if (rep > 0) {
rep--;
st = time
} else {
return false;
}
}
requestAnimationFrame(anim);
});
&#13;
#note {
color: red;
}
html {
font-family: Arial;
font-size: 1em;
}
&#13;
<h1>Tested mode</h1>
<h2>
- Chrome<br/> - NodeJS 6.x<br/> - IE Edge <br/>
</h2>
<p id="note">Please not teach me by not related answer like use selector outside frame-loop, optimize it, reduce relayout, cache node</p>
<body>
</body>
&#13;
由于
答案 0 :(得分:0)
函数变量不能跨函数共享