JS里的prototype对JS类(方法,函数,function)进行一个扩展吧!

JS里的prototype对JS类(方法,函数,function)进行一个扩展吧!,第1张

我们知道C#里有继承,类也有扩展方法,而在JS世界有是否也有相似的概念和功能实现呢,答案是肯定的,现在我来和大家一起聊聊这方面的知识吧!:P

   <script type="text/javascript">
 
        // 注意,prototype只对本script段起作用
        // prototype原型关键字,为一个JS原对象扩展一个方法
        Array.prototype.max = function() {
            var i, min = this[0];
            for (i = 1; i < this.length; i++) {
                if (min > this[i])
                    min = this[i];
            }
            return min;
        };
 
        var myArray = new Array();
        myArray[0] = 1;
        myArray[1] = 3;
        myArray[2] = 2;
        document.write(myArray.min());
    </script>

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2082737.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存