* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
		
			
				
	
	
	
		
			1.8 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.8 KiB
		
	
	
	
	
	
	
	
title, localeTitle
| 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://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with
https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/