javascript 对象引用错误
a javascript Object reference bug
我遇到这样的错误:
let needUploadFiles: string[] = [],
needUploadPages: Filmstrip[] =[],
needUploadContents: string[] = [];
Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {
if (Page.pages[index]) {
filmstrip.content = Page.pages[index].cleanHTML();
}
let hash = Lib.sha1(filmstrip.content),
content: string = filmstrip.content;
if (filmstrip.hash != hash) {
needUploadFiles.push(hash);
needUploadPages.push(filmstrip);
needUploadContents.push(content);
}
});
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
needUploadPages[index].hash = hash;
}
);
我想要零钱Win.dataSource.filmstrips.hash;
通常没问题。
但有时需要UploadPages[index].hash不能改Win.dataSource
我在 Promise.uploadFiles 中添加控制台,例如:
我改变了 Win.dataSource.filmstrips,
制作 Win.dataSource.filmstrips.length = 1 检查;
出现错误时:
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
console.log(hash);
needUploadPages[index].hash = hash;
console.log(needUploadPages[index].hash == hash)//true
console.log(Win.dataSource.filmstrips[index].hash == hash)//false
console.log(Win.dataSource.filmstrips[index] === needUploadPages[index])// false
}
);
然后我改成这样:
let needUploadPages: Filmstrip[] =[],
needUploadContents: string[] = [],
needUploadIndex: number[] = [];
Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {
if (Page.pages[index]) {
filmstrip.content = Page.pages[index].cleanHTML();
}
let hash = Lib.sha1(filmstrip.content),
content: string = filmstrip.content;
if (filmstrip.hash != hash) {
needUploadPages.push(filmstrip);
needUploadContents.push(content);
needUploadIndex.push(index);
}
});
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
Win.dataSource.filmstrips[needUploadIndex[index]].hash = hash;
}
);
这不是错误。
我不知道为什么。
为什么有时对有时错;
我希望得到这个答案。
谢谢你们,
我英文不好,抱歉
对象引用工作正常。试试这个简单的例子:
const origObjects = [{a: 1}, {a: 2}, {a: 3}]
const newObjects = []
origObjects.forEach(obj => newObjects.push(obj))
newObjects[1].a = 55
console.log(origObjects[1]) // Results in {a: 55}
我会说你在数组和索引的某处有错误。尝试console.log
数组并比较它们是否具有相同的内容。
我遇到这样的错误:
let needUploadFiles: string[] = [],
needUploadPages: Filmstrip[] =[],
needUploadContents: string[] = [];
Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {
if (Page.pages[index]) {
filmstrip.content = Page.pages[index].cleanHTML();
}
let hash = Lib.sha1(filmstrip.content),
content: string = filmstrip.content;
if (filmstrip.hash != hash) {
needUploadFiles.push(hash);
needUploadPages.push(filmstrip);
needUploadContents.push(content);
}
});
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
needUploadPages[index].hash = hash;
}
);
我想要零钱Win.dataSource.filmstrips.hash;
通常没问题。
但有时需要UploadPages[index].hash不能改Win.dataSource
我在 Promise.uploadFiles 中添加控制台,例如:
我改变了 Win.dataSource.filmstrips,
制作 Win.dataSource.filmstrips.length = 1 检查;
出现错误时:
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
console.log(hash);
needUploadPages[index].hash = hash;
console.log(needUploadPages[index].hash == hash)//true
console.log(Win.dataSource.filmstrips[index].hash == hash)//false
console.log(Win.dataSource.filmstrips[index] === needUploadPages[index])// false
}
);
然后我改成这样:
let needUploadPages: Filmstrip[] =[],
needUploadContents: string[] = [],
needUploadIndex: number[] = [];
Win.dataSource.filmstrips.forEach((filmstrip: Filmstrip, index) => {
if (Page.pages[index]) {
filmstrip.content = Page.pages[index].cleanHTML();
}
let hash = Lib.sha1(filmstrip.content),
content: string = filmstrip.content;
if (filmstrip.hash != hash) {
needUploadPages.push(filmstrip);
needUploadContents.push(content);
needUploadIndex.push(index);
}
});
Promise.uploadFiles(
needUploadFiles, needUploadContents,
(hash: string, index: number) => {
Win.dataSource.filmstrips[needUploadIndex[index]].hash = hash;
}
);
这不是错误。
我不知道为什么。
为什么有时对有时错;
我希望得到这个答案。
谢谢你们,
我英文不好,抱歉
对象引用工作正常。试试这个简单的例子:
const origObjects = [{a: 1}, {a: 2}, {a: 3}]
const newObjects = []
origObjects.forEach(obj => newObjects.push(obj))
newObjects[1].a = 55
console.log(origObjects[1]) // Results in {a: 55}
我会说你在数组和索引的某处有错误。尝试console.log
数组并比较它们是否具有相同的内容。