返回Javascript中未定义的对象属性
我有以下代码:返回Javascript中未定义的对象属性,javascript,recursion,Javascript,Recursion,我有以下代码: function Tree() { this.capacity = 1; this.contents = 0; this.children = []; this.divided = false; this.pour = function(amount) { this.contents += amount; if (this.contents <= 1) { return;
function Tree() {
this.capacity = 1;
this.contents = 0;
this.children = [];
this.divided = false;
this.pour = function(amount) {
this.contents += amount;
if (this.contents <= 1) {
return;
}
if (!this.divided) {
this.divide();
}
amount = this.contents - 1;
this.contents = 1;
for (let child in this.children) {
this.children[child].pour(amount * .5);
}
}
this.divide = function() {
this.children = new Array(2).fill(0).map(x => new Tree());
this.divided = true;
return;
}
this.getContents = function(row, currentRow) {
if (currentRow === undefined) {
currentRow = 0;
}
if (row === currentRow) {
console.log('Contents:', this.contents)
return this.contents;
}
if (this.divided) {
console.log(row, currentRow)
currentRow++;
this.children[0].getContents(row, currentRow);
} else {
return;
}
}
}
它应该返回1,因为第二行内容是1。它在控制台中记录1,但不返回正确的值。我很好奇到底出了什么问题
编辑:我想把它换成一个类,但它并没有解决问题:
class Tree {
constructor() {
this.capacity = 1;
this.contents = 0;
this.children = [];
this.divided = false;
}
pour(amount) {
this.contents += amount;
if (this.contents <= 1) {
return;
}
if (!this.divided) {
this.divide();
}
amount = this.contents - 1;
this.contents = 1;
for (let child in this.children) {
this.children[child].pour(amount * .5);
}
}
divide() {
this.children = new Array(2).fill(0).map(x => new Tree());
this.divided = true;
return;
}
getContents(row, currentRow) {
if (currentRow === undefined) {
currentRow = 0;
}
if (row === currentRow) {
console.log('Contents:', this.contents)
return this.contents;
}
if (this.divided) {
console.log(row, currentRow)
currentRow++;
this.children[0].getContents(row, currentRow);
} else {
return;
}
}
}
类树{
构造函数(){
该容量=1;
此值为0;
这是:children=[];
此项=错误;
}
倾注(数量){
本条内容+=金额;
如果(this.contents new Tree());
这是真的;
返回;
}
getContents(行,当前行){
如果(currentRow==未定义){
currentRow=0;
}
如果(行===当前行){
console.log('Contents:',this.Contents)
返回此.contents;
}
如果(本部分){
console.log(行,当前行)
currentRow++;
this.children[0].getContents(行,当前行);
}否则{
返回;
}
}
}
您看到的控制台.log
是此调用的结果:
if (this.divided) {
console.log(row, currentRow)
currentRow++;
this.children[0].getContents(row, currentRow); //<-- here
// this is calling getContents() but ignores the return value
您看到的
console.log
是此调用的结果:
if (this.divided) {
console.log(row, currentRow)
currentRow++;
this.children[0].getContents(row, currentRow); //<-- here
// this is calling getContents() but ignores the return value
它将1记录到控制台,因为您调用
tree.pour(10)
当前行未定义,因为未在参数中传递它
if (currentRow === undefined) {
currentRow = 0;
}
if (row === currentRow) {
console.log('Contents:->', this.contents)
return this.contents;
}
这意味着currentRow为0,而row(1)不等于currentRow(0),这就是它返回undefined的原因
} else {
return;
}
它将1记录到控制台,因为您调用
tree.pour(10)
当前行未定义,因为未在参数中传递它
if (currentRow === undefined) {
currentRow = 0;
}
if (row === currentRow) {
console.log('Contents:->', this.contents)
return this.contents;
}
这意味着currentRow为0,而row(1)不等于currentRow(0),这就是它返回undefined的原因
} else {
return;
}
我没有看到任何构造函数考虑您的语法(
newtree()
),我对这意味着什么感到困惑,因为我认为这就是我对代码所做的。这让我很困惑,因为我可以访问属性中的值,但无法返回它们。也许是时候快速刷新@TheIncorrigible1-@Mark\u M$CURRENT\u YEAR
,Mark\u M,$CURRENT\u YEAR
!脱掉ES1998的外衣,拥抱真正的OOP类(/s,有点)我没有看到任何构造函数考虑到你的语法(newtree()
),我对这意味着什么感到困惑,因为我认为这就是我对代码所做的。这让我很困惑,因为我可以访问属性中的值,但无法返回它们。也许是时候快速刷新@TheIncorrigible1-@Mark\u M$CURRENT\u YEAR
,Mark\u M,$CURRENT\u YEAR
!脱掉ES1998的外衣,拥抱真正的OOP类(/s,有点),那么当行===currentRow返回正确的结果时,我该怎么做呢。是结果,而预期值为1,而不是未定义。它将。但是currentRow
不等于1,直到之后如果(this.divided)
那么当row==currentRow返回正确的结果时,我如何使它这样做,这就是我认为我正在做的。是结果,而预期值为1,而不是未定义。它将。但是currentRow
不等于1,直到之后if(this.divided)