prototype 允许您向数组添加属性和方法。
主要语法:
Array.prototype.myUcase = function() {//this.length;this[i];}
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myUcase();
测试代码:
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>js数组属性 prototype</title>
</head>
<body>
<H1>prototype 允许您向数组添加属性和方法。</H1>
<p id="demo"></p>
</body>
</html>
<script>
/**** 添加新方法 ****/
Array.prototype.myUcase = function() {
for (let i = 0; i < this.length; i++) {
this[i] = this[i].toUpperCase();
}
};
/**** 在任何数组上使用该方法 ****/
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myUcase();
document.getElementById("demo").innerHTML = fruits;
</script>
网友评论