Is it okay to use with()?

后端 未结 3 1934
天命终不由人
天命终不由人 2021-01-19 10:51

Once, I saw an example like this:

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

And

3条回答
  •  再見小時候
    2021-01-19 11:56

    In his excellent book "Javascript: The Good Parts", Douglas Crockford lists the "with Statement" in Appendix B: The Bad Parts.

    He says "Unfortunately its results can sometimes be unpredictable, so it should be avoided".

    He goes on to give an example, where an assignment inside the with will operate on different variables depending on whether the object is defined or not.

    See With statement considered harmful (but less detailed than the explanation in the book).

提交回复
热议问题