gpt4 book ai didi

Javascript:数组没有改变,这是错误吗?

转载 作者:行者123 更新时间:2023-11-28 04:19:27 25 4
gpt4 key购买 nike

使用 [...].splice(index, 1,change)[...][index] =change 方法都不会更改数组。请参阅示例以获取更多信息。

注意:我了解js,除了t === 1之外,它在任何地方都会改变。谁能解释一下这种行为,这是一个已知的错误,还是有解决方案?

我想找到解决方案,因为我想创建支持相对字符串的“补间插值器库”(稍后支持对象、数组、数字,我将添加)。

// 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);
});
#note {
color: red;
}

html {
font-family: Arial;
font-size: 1em;
}
<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>

谢谢

最佳答案

函数变量不能跨函数共享

关于Javascript:数组没有改变,这是错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com