Javascript是否通过参考?

Javascript是否通过参考?

Javascript是传递引用还是传递值?下面是一个来自JavaScript:好的部分。我很困惑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());


萧十郎
浏览 594回答 4
4回答

沧海一幻觉

原语通过值传递,对象通过“引用的副本”传递。具体来说,当您传递一个对象(或数组)时,您正在(无形地)传递对该对象的引用,并且可以修改内容对象的引用,但如果试图覆盖引用,它不会影响调用方持有的引用的副本-即引用本身是通过值传递的:function replace(ref) {     ref = {};           // this code does _not_ affect the object passed}function update(ref) {     ref.key = 'newvalue';  // this code _does_ affect the _contents_ of the object}var a = { key: 'value' };replace(a);      // a still has its original value - it's unmodfiedupdate(a);   // the _contents_ of 'a' are changed

慕村225694

把它想成这样:每当您创建一个对象在ECMAScript中,这个对象是以神秘的形式形成的。ECMAScript通用场所没有人能得到的地方。你得到的只是一个参照系在这个神秘的地方。var obj = { };平obj仅是对象的引用(该对象位于该特殊的奇妙位置),因此,您只能传递以下内容参照系四处转转。实际上,任何访问OBJ将修改对象很远的地方。

开满天机

与C一样,最终,一切都是通过值传递的。与C不同,实际上不能备份并传递变量的位置,因为它没有指针,只有引用。它所拥有的引用都是对象,而不是变量。有几种方法可以实现相同的结果,但它们必须手工完成,而不仅仅是在调用或声明站点添加关键字。
打开App,查看更多内容
随时随地看视频慕课网APP