简体   繁体   中英

using an section of deep object keys as local variables

I don't know what this would be called but, suppose I have:

var object = {something:{really:{long:{x:2,y:3,z:null}}}};

//is there a way I can go like:

with (object.something.really.long) {
    z = x * y;
}

console.log(object);
//output: {something:{really:{long:{x:2,y:3,z:6}}}}

Your code works! There even isn't anything needed to change. However, as stated by MDN Docs , you should avoid using with as this would cause bug easily due to ambiguity.

Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues.

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.

Also, the object comparison would be false as object compare by reference. Anyway, the object will looks like what you said finally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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