Javascript中prototype属性实现给内置对象添加新的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Javascript中prototype属性实现给内置对象添加新的方法

永远爱好写程序   2020-05-13 我要评论

本文实例讲述了Javascript中prototype属性实现给内置对象添加新的方法。分享给大家供大家参考。具体实现方法如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prototype属性使用(给内置对象添加新的方法,方便调用)</title>
<script type="text/javascript">
function getMaxFunc() {
  var max = this[0];
  for (var i in this) {
    if (max < this[i]) {
      max = this[i];
    }
  }
  return max;
}
Array.prototype.getMax = getMaxFunc;
//Array是Javascript的内置对象,这里使用prototype定义一个新的方法getMax
var myArr = [3, 5, 6, 7, 9];
var max = myArr.getMax();
//这里就可以直接使用myArr.getMax了,像使用内置对象的方法一样使用
alert("max=" + max);
</script>
</head>
<body>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们