What does “with” do in JavaScript?

前端 未结 8 1657
北荒
北荒 2020-12-28 18:06

I saw JavaScript code which begins with with. That\'s a bit confusing. What does it do and how can it be used correctly?

with (sObj) return opti         


        
8条回答
  •  自闭症患者
    2020-12-28 18:27

    the with statement is pure syntactical sugar, but it also can cause some nasty bugs.

    See with Statement Considered Harmful for clarification:

    If you can't read a program and be confident that you know what it is going to do, you can’t have confidence that it is going to work correctly. For this reason, the with statement should be avoided.

提交回复
热议问题