JavaScript 09
4169 ワード
<!DOCTYPE html>
<html>
<head>
<title>Javascript 9</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
/*
* 。
*/
var arr = [ "nba", "haha", "cba", "aaa", "abc" ];
var arr2 = [ "qq", "xiaoqiang", 70 ];
document.write(arr + "<br>");
document.write(arr2 + "<br>");
// arr "mm", arr2 。
var newArr = arr.concat("mm", arr2);// mm , arr2 。
document.write(newArr + "<br>");
document.write(arr.join("-") + "<br>");
// join 。
function myJoin(arr, separator) {
var str = "";
for (var x = 0; x < arr.length; x++) {
if (x != arr.length - 1)
str += arr[x] + separator;
else
str += arr[x];
}
return str;
}
// , 。pop
document.write("<hr/>");
document.write(arr + "<br>");
document.write(arr.pop() + "<br>");// 。
document.write(arr + "<br>");
document.write(arr.reverse() + "<br>");
document.write(arr.shift() + "<br>");// 。
document.write(arr + "<br>");
document.write(arr.sort() + "<br>");
document.write("<hr/>");
var temp = arr.splice(1, 3, 8080, 9527, "xixixi", "wangcai");// 。
document.write(temp + "<br>");
document.write(arr + "<br>");
document.write("<hr/>");
document.write(arr.unshift("uuuu") + "<br>");
document.write(arr + "<br>");
</script>
</body>
</html>
練習:/*
* 。
*/
Array.prototype.getMax = function() {
var temp = 0;
for (var x = 1; x < this.length; x++) {
if (this[x] > this[temp]) {
temp = x;
}
}
return this[temp];
};
/*
* 。 toString 。 java 。
*/
Array.prototype.toString = function() {
return "[" + this.join(", ") + "]";
};
<!DOCTYPE html>
<html>
<head>
<title>Javascript 4</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="/test.js"></script>
</head>
<body>
<script type="text/javascript">
/*
* 。
* js 。
*/
//
var arr = [];
//arr.unshift("abc1","abc2","abc3");
arr.unshift("abc1");
arr.unshift("abc2");
arr.unshift("abc3");
document.write(arr+"<br>");
document.write(arr.pop()+"<br>");
document.write(arr.pop()+"<br>");
document.write(arr.pop()+"<br>");
//
var arr2 = [];
arr2.unshift("abc1");
arr2.unshift("abc2");
arr2.unshift("abc3");
document.write(arr2+"<br>");
document.write(arr2.shift()+"<br>");
document.write(arr2.shift()+"<br>");
document.write(arr2.shift()+"<br>");
</script>
<script type="text/javascript">
/*
* , 。
*/
var array = [ "nba", "haha", "cba", "aaa", "abc" ];
var maxValue = array.getMax();
document.write("maxValue:" + maxValue + "<br>");
document.write(array.toString() + "<br>");
</script>
</body>
</html>