javascript當(dāng)中prototype用法
prototype
見上一節(jié),馬克-to-win:prototype作用就是給某個(gè)類增加一個(gè)實(shí)例方法。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
例 3.6.2
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
/*When the Global object is created, it always has at least the following properties:
Object object Function object Array object String object
Boolean object Number object Date object Math object
Value properties
*/
Array.prototype.mymethod = function(number)
{
var result = -1;
/*注意mymethod功能是找出某數(shù)在數(shù)組中出現(xiàn)的位置。作為Array的一個(gè)function,可以訪問Array的屬性this.length, 參見上一個(gè)prototype的例子,
Student.prototype.infop = function()/*此方法可以為所有Student對象所用*/
{
document.writeln("對象的qixy屬性p:" + this.qixy);
document.writeln("對象的age屬性p:" + this.age);
/*下列話是合法的, 因?yàn)椴皇莟his.number, 而是Student.number*/
document.writeln("static method is " + Student.number);
};
*/
for (var i = 0; i < this.length; i ++)
{
if (this[i] == number)
{
result = i;
break;
}
}
return result;
}
var a = [3,5,23,4,67];
document.writeln(a.mymethod(4));
var zhanwei = "zhanwei";
</script>
輸出結(jié)果:
3