5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2009-09-05
http://walktree.5d.cn/
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2005/06/09 | Flash 中的数组的关联
类别(:: Flash ::)
|
评论
(0)
|
阅读(162)
|
发表于 11:42
var array_ = new Array(1, 2, 3);
var str = array_;
trace(str);
trace(array_);
str.push(4);
trace(str);
trace(array_);
str.reverse();
trace(str);
trace(array_);
输出为:
1,2,3
1,2,3
1,2,3,4
1,2,3,4
4,3,2,1
4,3,2,1
数组变量很像c语言..中的指针,这应该算是 flash 中的一个 bug..
看到了解决方法,思路是逐一复制:
----------------------------------------------------------------------------------------
来自闪吧
Object.prototype.duplicate = function() { //我想应该没有人会在object中建立object了吧
var b = new Object();
for (var j in this) {
b[j] = this[j];
}
return b;
};
Array.prototype.duplicate = function() {
var repetition:Array = new Array();
for (var element in this) {
if (this[element] instanceof Array) {
repetition[element] = this[element].duplicate();
} else if (this[element] instanceof Object) {
repetition[element] = this[element].duplicate();
} else {
if (!(this[element] instanceof Function)) {
repetition[element] = this[element];
}
}
}
return repetition;
};
var myArray:Array = new Array();
myArray[0] = [1, 2];
myArray[1] = {x:3, y:4};
myArray[2] = [5, [6, {a:21, b:22}]];
var temp:Array = myArray.duplicate();
trace(myArray[2][1][1].a);
trace(temp[2][1][1].a);
myArray[2][1][1].a = 8;
trace(myArray[2][1][1].a);
trace(temp[2][1][1].a);
0
评论
Comments
日志分类
首页
[226]
:: Flash ::
[87]
:: php ::
[8]
::眼睛和耳朵::
[36]
::键盘时代::
[73]
::水痕专栏::
[22]