JavaScript数组prototype属性示例
下面的例子中将把数组的元素更改为大写。
我们通过给原型添加一个myUcase函数。然后调用这个函数实现。
<!DOCTYPE html> <body style ="background-color:green"> prototype属性示例 <button onclick="myFunction()">CLICK</button> <script type="text/javascript"> Array.prototype.myUcase = function () { for (i = 0; i < this.length; i++) { this[i] = this[i].toUpperCase(); } } function myFunction() { var array = ["boo", "bill", "james", "tom"]; array.myUcase(); var x = document.getElementById("menu"); x.innerHTML = array; } </script> </body> </html>
JavaScript数组prototype属性
- 如前所述,为了在单个变量中存储多个值,我们使用array对象。
- 数组对象具有属性称为prototype属性。
- 在Length属性的帮助下,我们可以为数组对象添加新的属性和方法。
JavaScript数组prototype属性语法
array.prototype.name=value
日期:2020-04-11 23:04:36 来源:oir作者:oir