JavaScript 数组原型构造函数
JavaScript 数组原型构造函数用于向数组对象添加新方法和属性。这些属性和方法将对每个数组可用。
以下是数组原型构造函数的代码:-
举例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample {
font-size: 20px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>JavaScript Array prototype Constructor</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to convert the string to upper and lowercase
alternatively
</h3>
<script>
Array.prototype.upperLower = function () {
for (let i = 0; i < this.length; i++) {
if (i % 2 == 0) this[i] = this[i].toUpperCase();
else this[i] = this[i].toLowerCase();
}
};
let fillEle = document.querySelector(".sample");
let arr = ["H", "E", "L", "L", "O"];
fillEle.innerHTML = arr;
arr.upperLower();
document.querySelector(".Btn").addEventListener("click", () => {
fillEle.innerHTML = arr;
});
</script>
</body>
</html>输出

单击“单击此按钮”按钮后,将调用自定义数组函数:-

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP