首页 > Web开发 > 详细

JS之 Prototype原型

时间:2017-02-26 16:46:32      阅读:156      评论:0      收藏:0      [点我收藏+]

Prototype注意的细节:
1. prototype是函数(function)的一个必备属性(书面一点的说法是"保留属性")(只要是function,就一定有一个prototype属性)
2. prototype的值是一个对象
3. 可以任意修改函数的prototype属性的值。
4. 一个对象会自动拥有prototype的所有成员属性和方法。

解释:Array.prototype.getMax相当于给每个Array对象都添加了一个求最大值的方法

    Array.prototype.getMax = function(){
        var max = this[0];
        for(var index = 1; index<this.length ; index++){
            if(this[index]>max){
                max = this[index];    
            }    
        }
        return max;
    }
    
    
    Array.prototype.searchEle = function(target){
        for(var i = 0 ; i<this.length ; i++){
            if(target==this[i]){
                return i;    
            }    
        }
        return -1;
            
    }
    


    //var arr = new Array(12,4,17,9);
    var arr = [12,4,17,9];
    var max = arr.getMax();
    var index = arr.searchEle(9);
    document.write("最大值:"+ max+"<br/>");
    document.write("索引值:"+ index+"<br/>");

 

JS之 Prototype原型

原文:http://www.cnblogs.com/mswangblog/p/6444754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!