[英]Array.splice() not behaving as expected
I'm having a issue with the Array.splice()
function. 我遇到了
Array.splice()
函数的问题。 When I add an object to an array, then splice it back out, it loses all its properties. 当我将一个对象添加到数组中,然后将其拼接回来时,它会丢失所有属性。 Why?
为什么?
// create a new object named myObj, test to see if all properties are intact
var myObj = {
prop1: 5,
prop2: 3,
prop3: 9
};
for(key in myObj) {
document.write(key + " <br>");
}
// they are, prepare a break-line
document.write("---<br>");
// okay, so I'm adding the object to a newly created array
var myArr = new Array();
myArr.push(myObj);
// watch what happens if I splice the obj back out of the array
var mySplicedObj = myArr.splice(0, 1);
// why doesn't this work?
document.write(mySpliceObj.prop1);
// this shows that myObj has lost all its properties when spliced!
for(key in mySplicedObj) {
document.write(key);
}
// how is this happening, and why?
splice()
返回一个数组,您可以使用mySplicedObj[0]
访问该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.