JavaScript 是否通过引用传递?
2022-08-29 23:16:16
JavaScript 是传递引用还是传递值?
下面是一个来自 JavaScript 的例子:The Good Parts。我对矩形函数的参数感到非常困惑。它实际上是在函数内部重新定义的。没有原始参考。如果我从函数参数中删除它,则内部区域函数无法访问它。my
undefined
这是一个关闭吗?但不返回任何函数。
var shape = function (config) {
var that = {};
that.name = config.name || "";
that.area = function () {
return 0;
};
return that;
};
var rectangle = function (config, my) {
my = my || {};
my.l = config.length || 1;
my.w = config.width || 1;
var that = shape(config);
that.area = function () {
return my.l * my.w;
};
return that;
};
myShape = shape({
name: "Unhnown"
});
myRec = rectangle({
name: "Rectangle",
length: 4,
width: 6
});
console.log(myShape.name + " area is " + myShape.area() + " " + myRec.name + " area is " + myRec.area());