[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/with [Back]  [Original]

with - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

with

Deprecated

Avoid using this feature in new projects. JavaScript strict mode prohibits the with statement. This feature may be a candidate for removal from web standards or browsers.

Consider using the following features instead: Destructuring.

: with . . .

with .

In this article

js
with (expression)
  statement
expression

. .

statement

. ({ ... }) .

. '' ' ' . .

js
foo; //   
foo.bar; // bar  

. .

js
const foo = { bar: 1 };
console.log(foo.bar);
// foo    .
// bar foo  .

, .

js
console.log(globalThis.Math === Math); // true

with . (in ) .

with this .

js
with ([1, 2, 3]) {
  console.log(toString()); // 1,2,3
}

@@unscopables , ( ) . Symbol.unscopables .

with . with .

  • : with . with . , .

  • : with JavaScript , . .

    js
    function f(x, o) {
      with (o) {
        console.log(x);
      }
    }
    

    f with x . x o.x f f . x . . .

  • : with . . .

    js
    function f(foo, values) {
      with (foo) {
        console.log(values);
      }
    }
    

    ECMAScript 5 f([1, 2, 3], obj) , with values obj . ECMAScript 2015 Array.prototype values ( values ). with values [1, 2, 3].values , .

    values Array.prototype[@@unscopables] values values . values , .

with

with Math . with PI cos sin . JavaScript Math .

js
let a, x, y;
const r = 10;

with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

with

with . with .

js
let a, x, y;
const r = 10;

{
  const { PI, cos, sin } = Math;
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}

IIFE with

IIFE .

js
const objectHavingAnEspeciallyLengthyName = { foo: true, bar: false };

if (((o) => o.foo && !o.bar)(objectHavingAnEspeciallyLengthyName)) {
  //   .
}

with

with . .

js
const namespace = new Proxy(
  {},
  {
    has(target, key) {
      // `console`      .
      if (key in globalThis) {
        return false;
      }
      //    .
      return true;
    },
    get(target, key) {
      return key;
    },
  },
);

with (namespace) {
  console.log(a, b, c); // "a" "b" "c"
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-with-statement


Web Proxy Viewer  |  New URL  |  Original Page