Home > Web Front-end > JS Tutorial > The prototype attribute in Javascript implements adding new methods to built-in objects_javascript skills

The prototype attribute in Javascript implements adding new methods to built-in objects_javascript skills

WBOY
Release: 2016-05-16 15:58:50
Original
1376 people have browsed it

The example in this article describes the implementation of the prototype attribute in Javascript to add new methods to built-in objects. Share it with everyone for your reference. The specific implementation method is as follows:

<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>
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template