repl.it:https://repl.it/BuXR/3
.slide{
width: 100%;
height: 150px;
overflow: hidden;
position: relative;
}
/*SLIDE1*/
.one{
width: 300px;
height: 150px;
background-color: red;
position: absolute;
animation-name: example1;
animation-iteration-count: infinite;
animation-duration: 3s;
animation-delay: 1.5s;
x-animation-timing-function: ease;
z-index: 3;
}
/* Chrome, Safari, Opera */
@keyframes example1 {
0% {left:0px; top:0px;}
100% {left:1024px; top:0px;}
}
/*SLIDE2*/
.two{
width: 300px;
height: 150px;
background-color: green;
position: absolute;
animation-name: example2;
animation-duration: 3s;
animation-delay: 3s;
animation-iteration-count: infinite;
x-animation-timing-function: ease;
z-index: 2;
}
/* Standard syntax */
@keyframes example2 {
0% {left:0px; top:0px;}
100% {left:1024px; top:0px;}
}
.three{
width: 300px;
height: 150px;
background-color: blue;
position: absolute;
animation-name: example3;
animation-duration: 3s;
animation-delay: 4.5s;
animation-iteration-count: infinite;
x-animation-timing-function: ease;
z-index: 1;
}
@keyframes example3 {
0% {left:0px; top:0px;}
100% {left:1024px; top:0px;}
}
我的问题是为什么str2正在改变,即使它没有被逆转?
这让我非常沮丧,但我猜到为什么会发生这种情况。 tmp只是指向原始str2的指针,当我在tmp上调用reverse()时,它实际上反转了str2。
即使这确实发生了什么,我仍然觉得这是一种非常违反直觉的语言工作方式。
答案 0 :(得分:12)
你的猜测是正确的。
tmp只是指向原始str2的指针,因此无论在str2上执行什么操作,它都将存储到内存中,当您访问tmp时,它会找到对str2的引用。
我猜到为什么会这样。 tmp只是一个指针 到原来的str2,当我在tmp上调用reverse()时,它实际上是 反转str2。
答案 1 :(得分:11)
发生这种情况的2个理由:
reverse()
方法反转数组到位,因此tmp
反转:ref tmp
和str2
是对同一个数组实例的引用,因此str2
也相反。答案 2 :(得分:5)
我的问题是为什么str2正在改变,即使它不存在 逆转吗?
由于str2
和tmp
是指向同一数组的引用。因此,对tmp
执行的任何操作都将在str2
也指向的字符串上执行。
如果你想让它指向另一个数组,那么只需尝试
var tmp = [].concat(str2);
答案 3 :(得分:0)
您需要在数组内存上创建一个新点:
Array.prototype.clone = function(){
return JSON.parse(JSON.stringify(this));
};
var arr = [1, 2, 3];
var arr_clone = arr.clone();
arr_clone.reverse();
console.log(arr_clone);
// [3,2,1]
console.log(arr);
// [1,2,3]