freeCodeCamp/guide/chinese/javascript/with/index.md

1.8 KiB
Raw Blame History

title localeTitle
With

JavaScript的with语句是在一个对象上编辑多个属性的简便方法。大多数开发人员不鼓励使用with ,您最好不要使用此关键字。

注意 "strict mode"禁止使用with

句法

with (expression) 
  statement 

示例用法

在JavaScript中您可以单独修改对象的属性如下所示

let earth = {}; 
 earth.moons = 1; 
 earth.continents = 7; 

with为您提供了修改对象属性的简写:

with (earth) { 
  moons = 1; 
  continents = 7; 
 } 

虽然这个例子很做作,你能理解的用例with更多,如果你有更大的物体象下面这样:

earth.continents.australia.geography.ocean = "Pacific"; 
 earth.continents.australia.geography.river = "Murray"; 
 earth.continents.australia.geography.mountain = "Kosciuszko"; 

备择方案

你不应该使用它with因为它有微妙的错误和兼容性问题。强烈推荐的方法是将对象分配给变量,然后修改变量的属性。以下是使用更大对象的示例:

let earth = { 
  continents: { 
    australia: { 
      geography: {} 
    } 
  } 
 }; 
 
 let geo = earth.continents.australia.geography; 
 
 geo.ocean = "Pacific"; 
 geo.river = "Murray"; 
 geo.mountain = "Kosciuszko"; 

试试看

https://repl.it/Mixg/5

更多信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with

https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/

http://2ality.com/2011/06/with-statement.html