返回到:JavaScript对象:JavaScript Array 对象
定义和用法
prototype 属性使您有能力向对象添加属性和方法。
当构建一个属性,所有的数组将被设置属性,它是默认值。
在构建一个方法时,所有的数组都可以使用该方法。
注意: Array.prototype 单独不能引用数组, Array() 对象可以。
注意: 在JavaScript对象中,Prototype是一个全局属性。
所有主要浏览器都支持prototype 属性。
语法
Array.prototype.name=value实例
一个新的数组的方法,将数组值转为大写:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(Web176.com)</title>
</head>
<body>
<p id="demo">点击按钮创建数组,并调用新建的 ucase() 方法, 最后显示结果。</p>
<button onclick="myFunction()">点我</button>
<script>
Array.prototype.myUcase=function()
{
for (i=0;i<this.length;i++)
{
this[i]=this[i].toUpperCase();
}
}
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.myUcase();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/jsarrtips/3396.html
支付宝
微信