是否有任何内置方法来更改JavaScript对象';将属性设置为某个值?

是否有任何内置方法来更改JavaScript对象';将属性设置为某个值?,javascript,object,Javascript,Object,我想将JavaScript对象的所有属性值更改为某个值(本例中为false)。我知道如何做到这一点,通过单独更改所有这些选项(示例A)或使用循环(示例B)。我想知道是否有其他内置的方法可以做到这一点,推荐的方法是什么(主要是在速度方面,或者是否有任何其他副作用) 伪代码: // Example object Settings = function() { this.A = false; this.B = false; this.C = false; // more

我想将JavaScript对象的所有属性值更改为某个值(本例中为false)。我知道如何做到这一点,通过单独更改所有这些选项(示例A)或使用循环(示例B)。我想知道是否有其他内置的方法可以做到这一点,推荐的方法是什么(主要是在速度方面,或者是否有任何其他副作用)

伪代码:

// Example object
Settings = function() {
    this.A = false;
    this.B = false;
    this.C = false;
    // more settings...
}

// Example A - currently working
updateSettingsExampleA = function(settings) {
    // Settings' properties may not be be false when called
    settings.A = false;
    settings.B = false;
    settings.C = false;
    while (!(settings.A && settings.B && settings.C) && endingCondition) {
        // code for altering settings
    }
}

// Example B - currently working
updateSettingsExampleB = function(settings) {
    // Settings' properties may not be be false when called
    for (var property in settings) {
        settings[property] = false;
    }
    while (!(settings.A && settings.B && settings.C) && endingCondition) {
        // code for altering settings
    }
}

// possible other built in method
updateSettingsGoal = function() {
    this.* = false; // <-- statement to change all values to false
    while (!(this.A && this.B && this.C) && endingCondition) {
        // code for altering settings
    }
}
//示例对象
设置=函数(){
这个.A=假;
这个.B=假;
这个.C=假;
//更多设置。。。
}
//示例A-当前正在工作
updateSettingsExampleA=函数(设置){
//调用设置时,设置的属性不能为false
设置。A=假;
设置。B=假;
设置。C=假;
while(!(settings.A&&settings.B&&settings.C)&&endingCondition){
//更改设置的代码
}
}
//示例B-目前正在工作
updateSettingsExampleB=函数(设置){
//调用设置时,设置的属性不能为false
用于(设置中的var属性){
设置[属性]=假;
}
while(!(settings.A&&settings.B&&settings.C)&&endingCondition){
//更改设置的代码
}
}
//可能的其他内置方法
updateSettingsGoal=函数(){

this.*=false;//否,没有这样的内置方法。如果要“将所有属性值更改为false”,请使用循环来执行此操作。您的示例B完全可以


我不建议展开循环(示例A),除非它不是“所有属性”,或者您需要此代码段的绝对最大速度。但这是一种微观优化,不会使您的代码变得更好。

我创建了一个jsPerf,有7种不同的方法(至少有一种是荒谬的)。单独分配属性似乎是目前为止最快的:@RickHitchcock:你已经被优化器打败了。检查啊,下次我必须记住!我想知道为什么重新初始化会比较慢。只需补充一点:对于整数索引,使用ES6将非常感谢,我也非常感谢你的jsperf示例