javascript中发生意外值更改 var li=[[1,3,4,5],[1,2,3,1],[3,5,6,2]; var m=r4dxy(0.1); var i=0; li[i]=m.ope(li[0]); 文件.编写(i);
为什么上面代码中的变量“i”会更改为5 这是文件中的一个错误,它在javascript中发生意外值更改 var li=[[1,3,4,5],[1,2,3,1],[3,5,6,2]; var m=r4dxy(0.1); var i=0; li[i]=m.ope(li[0]); 文件.编写(i);,javascript,Javascript,为什么上面代码中的变量“i”会更改为5 这是文件中的一个错误,它在matrixMul方法的下面循环中创建了一个全局变量i <body> <script src="http://www.cse.msu.edu/~zhangh40/w/wgl.js"></script> <script> var li = [[1,3,4,5],[1,2,3,1],[3,5,6,2]]; var m = r4dxy(0.1); var i =
matrixMul
方法的下面循环中创建了一个全局变量i
<body>
<script src="http://www.cse.msu.edu/~zhangh40/w/wgl.js"></script>
<script>
var li = [[1,3,4,5],[1,2,3,1],[3,5,6,2]];
var m = r4dxy(0.1);
var i = 0;
li[i] = m.ope(li[0]);
document.write(i);
</script>
因为中的代码编写不正确
该代码的功能是:
function matrixMul(other) {
var i, a, b;
if (this.c != other.r)
return false;
li = [];
for (i = 1; i <= this.r * other.c; i++) {
a = (i % other.c == 0) ? other.c : (i % other.c);
b = Math.floor((i - 1) / other.c + 1);
sum = 0;
for (j = 1; j <= other.r; j++)
sum += (this.at(b, j) * other.at(j, a));
li.push(sum);
}
return new matrix(li, this.r, other.c);
}
函数矩阵MUL(其他){
if(this.c!=other.r)
返回false;
li=[];
对于(i=1;Ilaso,变量a
、b
、li
、和sum
也需要var
。
for (var i = 1; i <= this.r * other.c; i++) {
function matrixMul(other) {
var i, a, b;
if (this.c != other.r)
return false;
li = [];
for (i = 1; i <= this.r * other.c; i++) {
a = (i % other.c == 0) ? other.c : (i % other.c);
b = Math.floor((i - 1) / other.c + 1);
sum = 0;
for (j = 1; j <= other.r; j++)
sum += (this.at(b, j) * other.at(j, a));
li.push(sum);
}
return new matrix(li, this.r, other.c);
}
function matrixMul(other){
if(this.c != other.r)
return false;
li = [];
for(i=1;i<=this.r*other.c;i++){
a = (i%other.c==0)?other.c:(i%other.c);
b = Math.floor((i-1)/other.c+1);
sum = 0;
for(j=1;j<=other.r;j++)
sum+=(this.at(b,j)*other.at(j,a));
li.push(sum);
}
return new matrix(li,this.r,other.c);
}