简体   繁体   English

有人可以向我解释这小段JavaScript代码吗?

[英]Can someone explain this small piece of javascript code to me?

Basically I expect console.log to output 'yeah' but it doesn't. 基本上,我希望console.log输出“是”,但不会。 What can I do to make it output yeah without directly referencing it inside of usefulFunction ? 我可以做些什么使它输出yeah而不直接在usefulFunction内部引用它?

App = {
    config: {
        updateThis: 'false'
    },
    init: function(){
        this.usefulFunction(this.config.updateThis);
        this.consoleIt();
    },
    usefulFunction: function(item){
        item = 'yeah';
        // swap the above line for the line below and it works as expected
        // this.config.updateThis = 'yeah';
    },
    consoleIt: function(){
        console.log(this.config.updateThis);
    }
}

App.init();

in usefulFunction , you are expecting the C++ style of pass by reference to affect the original reference to config.updateThis , however, when you call usefulFunction ,您期望按引用传递的C ++样式会影响对config.updateThis的原始引用,但是,当您调用

 this.usefulFunction(this.config.updateThis);

You are creating a new reference to the 'false' string (to pass to usefulFunction ), and you can't update the original reference in this.config from usefulFunction . 您正在创建对'false'字符串的新引用(以传递给usefulFunction ),并且您无法从usefulFunction更新this.config的原始引用。

The only way to address this is to pass the name of the object to update. 解决此问题的唯一方法是传递对象名称以进行更新。 Again, there is no C++ pass by reference in JS. 同样,JS中没有C ++通过引用传递。 Working example 工作实例

App = {
    config: {
        updateThis: 'false'
    },
    init: function(){
        this.usefulFunction(this.config, 'updateThis');
        this.consoleIt();
    },
    usefulFunction: function(object, prop){
        object[prop] = 'yeah';
    },
    consoleIt: function(){
        console.log(this.config.updateThis);
    }
}

The Problem Is NOT That Strings are immutable 问题不在于字符串是不可变的

ᚗ̸̢̛͝ claims that the problem is that strings are immutable; ᚗ̸̢̛͝声称问题在于字符串是不可变的; however, the problem is deeper than that. 然而,问题比这更深。 The fact that strings are immutable means you can't change the current reference (and therefore have all other references update), but even if they were mutable, you couldn't just set a separate reference and affect existing references 字符串是不可变的事实意味着您无法更改当前引用(因此所有其他引用都将进行更新),但是即使它们是可变的,您也不能仅设置单独的引用并影响现有的引用

var a = {b:1};
function change(obj) {
    // This is assigning {c:2} to obj but not to a
    // obj and a both point to the same object, but 
    // the following statement would simple make obj point to a different object
    // In other languages, you could define function(&obj) {}
    // That would allow the following statement to do what you intended
    obj = {c:2};
}

change(a);
console.log(a); // still {b:1}

You can pass an object (as opposed to a string object) to your function. 您可以将一个对象(与字符串对象相对)传递给函数。 The reason for this is because JavaScript strings are immutable. 这样做的原因是因为JavaScript字符串是不可变的。

Primitive values 原始值

All types except objects define immutable values. 除对象外的所有类型均定义不可变值。 Specifically, strings are immutable (unlike in C for instance). 具体来说,字符串是不可变的(例如,与C不同)。 We refer to values of these types as "primitive values." 我们将这些类型的值称为“原始值”。 This is explained in more detail in the section on Strings below. 这将在下面有关字符串的部分中详细说明。

Source: https://developer.mozilla.org/en-US/docs/JavaScript/Data_structures 资料来源: https : //developer.mozilla.org/en-US/docs/JavaScript/Data_structures


If you want to pass something mutable to your function, pass an object. 如果要将可变的东西传递给函数,请传递一个对象。

// Calling code
this.usefulFunction(this.config);

// Useful function isn't very useful
usefulFunction: function(config) {
   config.updateThis = "some new value";
}

Going back to your example, updating a config object via a function. 回到您的示例,通过函数更新配置对象。

// Calling code
this.usefulFunction("updateThis", "some new value");

// Useful function is a bit useful
usefulFunction: function(name, value) {
    this.config[name] = value;
}

Assuming your code actually works: 假设您的代码实际可行:

App = {
    config: {
        updateThis: 'false'
    },
    init: function(){
        this.config.updateThis=this.usefulFunction( this.config.updateThis);
        this.consoleIt();
    },
    usefulFunction: function(item){
       return item = 'yeah';
        // swap the above line for the line below and it works as expected
        // this.config.updateThis = 'yeah';
    },
    consoleIt: function(){
        console.log(this.config.updateThis);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM