文章目录
- 76 数组的方法
- 77 数组练习
- 78 数组的剩余方法
- 79 数组的方法
- 80 arguments
76 数组的方法
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">var arr = ["孙悟空", "猪八戒", "沙和尚", "白龙马", "唐三藏"];var res = arr.slice(0, 2); res = arr.slice(1); res = arr.slice(1, -2); var arr = ["孙悟空", "猪八戒", "沙和尚", "白龙马", "唐三藏"];var res = arr.splice(0, 2); console.log(arr);res = arr.splice(0, 2, "牛魔王", "铁扇公主"); res = arr.splice(0, 0, "牛魔王", "铁扇公主");
</script>
<style>
</style>
</head>
<body>
</body>
</html>
77 数组练习
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">var arr = [1, 2, 3, 2 ,1, 3, 4, 2, 5];for(var i = 0; i < arr.length; i++){for(var j = i + 1; j < arr.length; j++){if(arr[j] == arr[i]){arr.splice(j, 1);j--;}}}document.write(arr);
</script>
<style>
</style>
</head>
<body>
</body>
</html>
78 数组的剩余方法
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">var arr = [1, 2, 3];var arr2 = [4, 5, 6];var arr3 = [7, 8, 9];var res = arr.concat(arr2); res = arr.concat(arr2, arr3, "牛魔王"); res = arr.join(); res = arr.join("-"); res = arr.join(""); arr.reverse(); arr = ["b", "d", "c", "a", "e"];arr.sort(); arr.reverse(); arr = [3, 4, 1, 2, 5];arr.sort(); arr = [3, 4, 11, 2, 5];arr.sort(); arr = [5, 4, 3];arr.sort(function(a, b){console.log(a); console.log(b);});arr.sort(function(a, b){if(a > b){return 1;}else if(a < b){return -1;}else{return 0;}return a - b;return b - a;});
</script>
<style>
</style>
</head>
<body>
</body>
</html>
79 数组的方法
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">function fun(){alert(this);}function fun(a, b){console.log(a);console.log(b);}var obj = {name:"obj",sayName: function(){alert(this.name);}};var obj2 = {name:"obj2"};fun.call();fun.apply();fun.call(obj); obj.sayName.apply(obj2); fun.call(obj, 2, 3); fun.apply(obj, 2, 3); fun.apply(obj, [2, 3]);
</script>
<style>
</style>
</head>
<body>
</body>
</html>
80 arguments
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">function fun(){console.log(arguments instanceof Array);console.log(Array.isArray(arguments));console.log(arguments.length);console.log(arguments[0]);console.log(arguments.callee); }fun(); fun("hello", true); fun("hello", true);
</script>
<style>
</style>
</head>
<body>
</body>
</html>