JavaScript是通过引用传递还是通过值传递?

下面是一个来自JavaScript: The Good Parts的例子。我对矩形函数的参数很困惑。它实际上是未定义的,并在函数内部重新定义。没有原始参考文献。如果我从函数参数中删除它,内部区域函数就不能访问它。

它是一个闭包吗?但是没有返回函数。

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());

当前回答

对象总是按引用传递,原语总是按值传递。只要将参数保持在对象的相同地址即可。

下面是一些代码来说明我的意思(在JavaScript沙箱中试试,比如https://js.do/)。

不幸的是,你不能只保留参数的地址;还保留了所有原始成员值。

a = { key: 'bevmo' };
testRetain(a);
document.write(' after function ');
document.write(a.key);


function testRetain (b)
{
    document.write(' arg0 is ');
    document.write(arguments[0].key);
    b.key = 'passed by reference';
    var retain = b; // Retaining the original address of the parameter

    // Address of left set to address of right, changes address of parameter
    b = {key: 'vons'}; // Right is a new object with a new address
    document.write(' arg0 is ');
    document.write(arguments[0].key);

    // Now retrieve the original address of the parameter for pass by reference
    b = retain;
    document.write(' arg0 is ');
    document.write(arguments[0].key);
}

结果:

Arg0是bevmo Arg0是vons Arg0是在引用传递的函数之后通过引用传递

其他回答

对象总是按引用传递,原语总是按值传递。只要将参数保持在对象的相同地址即可。

下面是一些代码来说明我的意思(在JavaScript沙箱中试试,比如https://js.do/)。

不幸的是,你不能只保留参数的地址;还保留了所有原始成员值。

a = { key: 'bevmo' };
testRetain(a);
document.write(' after function ');
document.write(a.key);


function testRetain (b)
{
    document.write(' arg0 is ');
    document.write(arguments[0].key);
    b.key = 'passed by reference';
    var retain = b; // Retaining the original address of the parameter

    // Address of left set to address of right, changes address of parameter
    b = {key: 'vons'}; // Right is a new object with a new address
    document.write(' arg0 is ');
    document.write(arguments[0].key);

    // Now retrieve the original address of the parameter for pass by reference
    b = retain;
    document.write(' arg0 is ');
    document.write(arguments[0].key);
}

结果:

Arg0是bevmo Arg0是vons Arg0是在引用传递的函数之后通过引用传递

与C语言一样,最终,所有内容都是通过值传递的。不像C语言,你不能实际备份和传递变量的位置,因为它没有指针,只有引用。

它所引用的都是对象,而不是变量。有几种方法可以达到相同的结果,但它们必须手工完成,而不仅仅是在调用或声明站点上添加关键字。

实际上,Alnitak是正确的,而且很容易理解,但最终在JavaScript中,一切都是通过值传递的。

对象的“值”是什么?它是对象引用。

当你传入一个对象时,你得到这个值的一个副本(因此Alnitak描述的“引用的副本”)。如果你改变了这个值,你不会改变原来的对象;您正在更改该引用的副本。

“全局”JavaScript变量是窗口对象的成员。您可以将引用作为窗口对象的成员访问。

var v = "initialized";

function byref(ref) {
  window[ref] = "changed by ref";
}

byref((function(){for(r in window){if(window[r]===v){return(r);}}})());
// It could also be called like... byref('v');
console.log(v); // outputs changed by ref

注意,上面的例子不适用于函数中声明的变量。

你可以这样想:

无论何时在ECMAscript中创建一个对象,这个对象都是在一个神秘的ECMAscript通用的地方形成的,没有人能够得到。你只能得到这个神秘地方的那个物体的参考。

var obj = { };

即使obj也只是对对象的引用(它位于那个特殊的位置),因此,你只能传递这个引用。实际上,任何访问obj的代码段都会修改很远很远的对象。