日期:2014-05-16  浏览次数:20413 次

js封装成对象的例子

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
?<meta charset="UTF-8">
?<title>Base page</title>
?
??? <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>


</head>
<body>
<script type="text/javascript">
?Array.prototype.unique = function() {
??var a = {};
??var len = this.length;
??for (var i = 0; i < len; i++) {
???if (typeof a[this[i]] == "undefined") {
????a[this[i]] = 1;
???}
??}
???
??this.length = 0;
??for (var i in a) {
???this[this.length] = i;
??}
???
??return this;
?}
??
?Array.prototype.max = function() {
??return Math.max.apply({}, this);
?}
??
?Array.prototype.min = function() {
??return Math.min.apply({}, this);
?}
?
?
?var arr = [7,3,9,7,6,2,4,2,8];
??? ?console.log(arr.unique());
??? ?console.log(arr.max());
??? ?console.log(arr.min());
??
</script>
</body>

</html>