Javascript 为什么闭包属性在nodejs中不起作用?

Javascript 为什么闭包属性在nodejs中不起作用?,javascript,node.js,closures,Javascript,Node.js,Closures,我是NodeJS的新手,正在学习一门课程。但是,我无法使javascript的简单闭包属性在其中工作。我有两个文件index.js和rectangle.js,其中我使用回调返回矩形的面积和周长 index.js var rect = require('./rectangle'); function solveRect(l,b) { console.log("Solving for rectangle with l = " + l + "and b = &q

我是NodeJS的新手,正在学习一门课程。但是,我无法使javascript的简单闭包属性在其中工作。我有两个文件index.js和rectangle.js,其中我使用回调返回矩形的面积和周长

index.js

var rect = require('./rectangle');

function solveRect(l,b) {
    console.log("Solving for rectangle with l = " + l + "and b = " + b);

    rect(l,b, (err,rectangle) => {
        if(err) {
            console.log("ERROR: " + err.message);
        }
        else {
            console.log("The area of rectangle of dimensions l = " 
                + l + "and b = " + b + " is "  + rectangle.area());

            console.log("The perimeter of rectangle of dimensions l = " 
                + l + "and b = " + b + " is "  + rectangle.perimeter());
        }
    });
    console.log("This statement is after the call to rect()");
}

solveRect(2,4);
solveRect(3,5);
solveRect(0,4);
solveRect(-3,-5);
module.exports = (x,y,callback) => {
    if( x <= 0 || y <= 0) {
        setTimeout(() =>
            callback(new Error("rectangle dimensions should be greater than zero"),
                null),
            2000
        );
    }
    else {
        setTimeout(() =>
            callback(null,
                {
                    perimeter: (x,y) => (2*(x+y)),
                    area: (x,y) => (x*y)
                }),
            2000
        );
    }
}
rectangle.js

var rect = require('./rectangle');

function solveRect(l,b) {
    console.log("Solving for rectangle with l = " + l + "and b = " + b);

    rect(l,b, (err,rectangle) => {
        if(err) {
            console.log("ERROR: " + err.message);
        }
        else {
            console.log("The area of rectangle of dimensions l = " 
                + l + "and b = " + b + " is "  + rectangle.area());

            console.log("The perimeter of rectangle of dimensions l = " 
                + l + "and b = " + b + " is "  + rectangle.perimeter());
        }
    });
    console.log("This statement is after the call to rect()");
}

solveRect(2,4);
solveRect(3,5);
solveRect(0,4);
solveRect(-3,-5);
module.exports = (x,y,callback) => {
    if( x <= 0 || y <= 0) {
        setTimeout(() =>
            callback(new Error("rectangle dimensions should be greater than zero"),
                null),
            2000
        );
    }
    else {
        setTimeout(() =>
            callback(null,
                {
                    perimeter: (x,y) => (2*(x+y)),
                    area: (x,y) => (x*y)
                }),
            2000
        );
    }
}
module.exports=(x,y,回调)=>{
if(x)
回调(null,
{
周长:(x,y)=>(2*(x+y)),
面积:(x,y)=>(x*y)
}),
2000
);
}
}
我发现,由于外部范围的回调已经可以使用长度和宽度,因此在调用rectangle.area()函数时,不需要传递它们


输出:我将NaN作为面积和周长返回,而不是实际计算的面积。

perimiter和
area
函数采用参数
x
y
,因此它们使用这些参数来计算结果,而不是从闭包继承的变量。由于在
solveRect()
中调用参数时没有提供任何参数,因此在
undefined
上执行算术运算,结果是
NaN

去掉参数,这样他们就可以使用闭包变量了

        setTimeout(() =>
            callback(null,
                {
                    perimeter: () => (2*(x+y)),
                    area: () => (x*y)
                }),
            2000
        );

perimiter
area
函数采用参数
x
y
,因此它们使用这些参数来计算结果,而不是从闭包继承的变量。由于在
solveRect()
中调用参数时没有提供任何参数,因此在
undefined
上执行算术运算,结果是
NaN

去掉参数,这样他们就可以使用闭包变量了

        setTimeout(() =>
            callback(null,
                {
                    perimeter: () => (2*(x+y)),
                    area: () => (x*y)
                }),
            2000
        );