00005.js数组属性 prototype

2022-02-17  本文已影响0人  笑着字太黑

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>
上一篇 下一篇

猜你喜欢

热点阅读