當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaScript fill()用法及代碼示例


fill() 是 JavaScript 中的預定義函數,用於用靜態值填充數組的所有元素。

例:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		//array
		var cities = ["New Delhi", "Mumbai", "Chennai", "Banglore"];
		//printing the values
		document.write("The cities are:" + cities);
		document.write("<br>");
		
		//filling all elements with a defualt city "Pune"
		cities.fill("Pune");
		//printing the values
		document.write("The cities are:" + cities);
		document.write("<br>");		
		
	</script>
</body>
</html>

輸出

The cities are:New Delhi,Mumbai,Chennai,Banglore
The cities are:Pune,Pune,Pune,Pune


相關用法


注:本文由純淨天空篩選整理自 fill() function with example in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。