javascript當(dāng)中concat,join,slice用法
1.3(concat,join,slice)
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<Script>
/* 馬克-to-win:qixy: Array is a dynamic array.
- myArray = new Array(aLength)
- myArray = new Array(anItem1, anItem2, anItem3, ...)
*/
var arr = new Array(1);//arr.length is 1,但是里面的東西是undefined, 所以這樣寫[undefined]
/*雖然arr是個(gè)object, 但是里面的東西是undefined*/
document.write("typeof arr is "+typeof(arr));
var a = new Array(2, 6, 5, "a");
document.write("arr.length is "+arr.length+"a.length is "+a.length);
var b = new Array(12, 14);
arr[0] = "java";
arr[1] = "intel";
arr[2] = "microsoft";
/* Property/method value type: Array object
JavaScript syntax: - myArray.concat(someValues, ...)
The result of this method is a new array consisting of the original array, plus the concatenation.
The values that are passed to the method are added to the end of the array.
If arrays are passed, they are flattened and their individual elements added.
The method returns an array consisting of the original Array plus the concatenated values.
If Array1 contains "AAA", "BBB", "CCC" and Array2 contains "000", "111", "222", then the method
call Array1.concat(Array2) will return an array with all the elements in a single collection. The
original arrays will be untouched.
*/
document.write("arr.toString() is " + arr.toString() + "<br>");
//與無(wú)參join()方法等同
var arrconcat=arr.concat(a, b);
document.write("arr.concat(a,b) is " + arrconcat + "<br>");
/*Array.join() (Method)
Concatenate array elements to make a string.
Property/method value type: String primitive
JavaScript syntax: - myArray.join(aSeparator)
Argument list: aSeparator A string to place between array elements as the array is concatenated to form a string.
//無(wú)參join()方法默認(rèn)是用逗號(hào)連接
*/
document.write("arr.join() is " + arr.join() + "<br>");
//無(wú)參join()方法默認(rèn)是用逗號(hào)連接
document.write("arr.join(' & ') is " + arr.join(' & ') + "<br>");
/* Array.reverse() (Method)
Reverse the order of array elements.
Property/method value type: Array object
*/
document.write("arr.reverse() is " + arr.reverse() + "<br>");
/*Array.sort() (Method)
Sort the elements in an array.
Property/method value type: Array object
JavaScript syntax: - myArray.sort()
*/
document.write("arr.sort() is " + arr.sort() + "<br>");
//如果調(diào)用該方法時(shí)沒有參數(shù),將按字母順序進(jìn)行排序
document.write("a is " + a + "<br>");
/* returned value type: Array object
JavaScript syntax: - myArray.slice(aRange)
a bit like substring, but substring is applicable to String, but slice is applicable to array.
allegedly,it includes former,exclude latter,一薄片面包:a slice of bread*/
var slicedArr = a.slice(0, 2);
document.write("a.slice(0,2) is " + slicedArr + "<br>");
//注意不包括 end 對(duì)應(yīng)的元素
//如果省略 end 將復(fù)制 start 之后的所有元素
document.write("a.slice(1) is " + a.slice(1) + "<br>");
</script>
</head>
<body>
</body>
</html>
輸出結(jié)果:
typeof arr is objectarr.length is 1a.length is 4arr.toString() is java,intel,microsoft
arr.concat(a,b) is java,intel,microsoft,2,6,5,a,12,14
arr.join() is java,intel,microsoft
arr.join(' & ') is java & intel & microsoft
arr.reverse() is microsoft,intel,java
arr.sort() is intel,java,microsoft
a is 2,6,5,a
a.slice(0,2) is 2,6
a.slice(1) is 6,5,a
例 1.4
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function sortNumber(a, b)
{
document.write(a + b);
}
function sortq(sortNumberqqq)
{
document.write("here");
sortNumberqqq(1, 2);
return 6;
}
/* 馬克-to-win:note that it will report error if we write the following statement as document.write("test "+sortq(sortNumberqixy));
note that here sortNumber is a function pointer.
*/
document.write("test " + sortq(sortNumber));
</script>
</HEAD>
<BODY>
</BODY>
</HTML>
輸出結(jié)果:
here3test 6