一、数组

|
1. 创建数组的方式:
- var arr=[1,2,3];字面量方式创建数组,里面放一个数字,也是数组,而不是代表数组长度(推荐使用)
- var arr=new Array(1,2,3);当里面只有一个参数时,代表创建的数组长度是多少;不能为小数,eg:
new Array(10.2);报错,超出边界
2. typeof [1,2]——>’object’
| | | | —- | —- | —- | | |

1.1 改变原数组的方法

在原数据的基础上进行修改

|
1. push往数组的最后面添加数据方法
1. pop从数组的最后面弹出数据方法 ——>arr.pop(),传参没有作用
1. unshift往数组的最前面添加数据方法
1. shift从数组的最前面弹出数据方法
1. reverse 返回的为原数组的取反值
1. splice(从第几位开始,截取几位,在切口处添加新的数据)
1. sort排序,读对数组进行排序,改变的为原数组
- 1.必须写两个形参
- 2.看返回值(1)当返回值为负数时,前面的数放在前面
(2)当返回值为正数时,前面的数放在后面
(3)为0,不动
- 3. var arr=[1,5,2,0,11];冒泡排序:1分别和5,2,0,11比;5和2,0,11比,…;
- 4.注意:比较的为位置,当位置数变了还是比较位置
- return a-b;升序 return b-a; 降序
| | | | —- | —- | —- | | |

  1. //1.模拟push()方法
  2. var arr=[5,6];
  3. var array=[0,2,4]
  4. Array.prototype.push=function (){
  5. for(var i=0;i<arguments.length;i++){
  6. //刚开始不知道谁的长度,this是谁调用就是谁
  7. this[this.length]=arguments[i]
  8. }
  9. }
  10. arr.push(1,2,3)
  11. console.log(arr);
  12. //2.splice内部原理
  13. splice = function (pos) {
  14. pos += pos > 0 ? 0 : this.length
  15. }
  16. arr.splice(-1,1)//当截取的为负数值时,在内部会有一个转换,加上数组的长度,为截取开始位
  17. //3.sort排序原理
  18. var arr = [1, 5, 2, 0, 11];
  19. arr.sort(function (a, b) {
  20. //a-b升序 b-a降序
  21. return a - b
  22. })
  23. console.log(arr); //[0,1,2,5,11]
  24. //4.利用sort随机排序数组,每次返回数组的排序不同
  25. var arr1 = [1, 2, 3, 4, 5, 6, 7];
  26. arr1.sort(function (a, b) {
  27. return Math.random() - 0.5
  28. })

**sort**

  1. //1.对象属性排序
  2. var a = {
  3. name: "小a",
  4. age: 20,
  5. like: "香蕉"
  6. }
  7. var b = {
  8. name: "小b",
  9. age: 10,
  10. like: "苹果"
  11. }
  12. var c = {
  13. name: "小c",
  14. age: 30,
  15. like: "葡萄"
  16. }
  17. var arr = [a, b, c]
  18. arr.sort(function (a, b) {
  19. return a.age - b.age
  20. })
  21. console.log(arr);
  22. //2.字节排序
  23. // 按字节长度排列数组
  24. var arr=['c的','a','da得到','ssss','水水水sderf']
  25. function bytesLength(str) {
  26. var num = str.length;
  27. for (var i = 0; i < str.length; i++) {
  28. // charCodeAt可计算ASCII码值,>255汉字(2字节)
  29. if (str.charCodeAt(i) > 255) {
  30. num++;
  31. }
  32. }
  33. return num;
  34. }
  35. arr.sort(function (a, b) {
  36. return bytesLength(a) - bytesLength(b)
  37. })
  38. //["a", "c的", "ssss", "da得到", "水水水sderf"]
  39. console.log(arr);

image.png

1.2 不改变原数组的方法

返回值为新数组,原数组没有变化


1. concat() —->拼接两个数组:arr.concat(arr2)—->arr和arr2都没有变化
1. toString() —>把数组转为字符串
1. slice(从第几位开始截取,截取到哪一位) —->返回截取到的新数组,拿一个变量接收,否则无意义(注意:slice(1)从第一位开始截取到最后 )
1. jion("-") 把数组转为字符串,并按照jion参数进行拼接
1. split()把字符串按照split的参数拆分成数组(字符串数组)

image.pngimage.pngimage.pngimage.png

1.3 类数组

类数组要求:
1. 类数组的属性名为数组的索引值
1. 类数组要有length属性—->一定要有
1. 类数组 "splice":Array.prototype.splice 方法后,变为数组形式[],不再是对象{}形式
1. "push":Array.prototype.push手动为类数组添加 push()方法
1. 类数组既可以当作数组来用,也可以当作对象来用——>类数组没有数组的所有方法,需要自己手动添加
1. arguments 就是类数组

1.image.png 2.image.png

  1. var obj={
  2. "0":"a",
  3. "1":"b",
  4. "2":"c",
  5. "length":3,
  6. "push":Array.prototype.push,
  7. "splice":Array.prototype.splice
  8. }
  9. // 类数组的push()原理
  10. Array.prototype.push=function(target){
  11. // 如果为类数组,this变为obj
  12. this[this.length]=target;
  13. this.length++;
  14. }
  15. obj.push("d");
  16. console.log(obj);
  17. //类数组联系
  18. var obj={
  19. "2":"a",
  20. "3":"b",
  21. "length":2,
  22. // push决定在哪一位加东西,由length决定
  23. "push":Array.prototype.push
  24. }
  25. obj.push('c')
  26. obj.push('d')
  27. console.log(obj);

1.3 封装typeof方法

  1. /**
  2. * 1.判断,如果为null则返回null
  3. * 2.判断是不是object,如果为object则判断是包装类还是Array或者Object
  4. */
  5. function myType(target) {
  6. // 1. 判断模板
  7. var template={
  8. '[object Array]':'array',
  9. '[object Object]':'object',
  10. // 因为原始值已经过滤,得到以下值为包装类new Number(111)等
  11. '[object Number]':'number---object',
  12. '[object String]':'string---object',
  13. '[object Boolean]':'boolean---object',
  14. }
  15. // 2. 判断是不是null
  16. if (target == null) {
  17. return null;
  18. }
  19. // 2.在这一步已经过滤了原始值,因为Object.prototype.toString.call(123)--->[object Number]
  20. if (typeof (target) == "object") {
  21. var str=Object.prototype.toString.call(target);
  22. return template[str]
  23. }else{
  24. //3.原始值,直接返回
  25. return typeof(target)
  26. }
  27. }

1.4 数组去重

把数组当作对象的属性名,给这个属性随便赋一个值,因为对象的属性名是不会重复的,所以判断对象的属性值是不是空,为空,就把这个数组值push进新数组中

  1. //把数组的值当作对象的属性名,对象的属性名是不会重复的,因此可实现去重
  2. var arraa = [1, 2, 3, 1, 2, 3]
  3. Array.prototype.unique = function () {
  4. var temp = {};
  5. // 返回的新数组
  6. var arr = [];
  7. for (var i = 0; i < this.length; i++) {
  8. // 如果为undefind-->对象中没这个值
  9. if (!temp[this[i]]) {
  10. // 这一步是需要的,当有值时才不会返回undefined
  11. temp[this[i]]="123"
  12. //temp[this[i]]=this[i]是不可以的,如果this[i]=0,不能去重
  13. arr.push(this[i])
  14. }
  15. }
  16. return arr;
  17. }

二、try…catch…/es5严格模式

2.1 try{ }catch(e){}

image.png
image.png

2.2 es5严格模式


1. 基于es3.0的+es5.0的新增方法 使用的规则
1. es5.0基于es3.0,他们之间会有冲突的部分
1. es5.0严格模式启用,那么es3.0和es5.0产生冲突的部分就是用es5.0;否则会用es3.0
1. 启用es5.0严格模式的方法"use strict" 写在所有代码最顶端(全局,函数局部都可以)
1. with 可以改变作用域链,会把with括号里面的参数变成作用域的最顶端AO(严格模式不支持)
1. arguments.callee严格模式不支持,只是不支持arguments的一些属性,arguments本身是可以使用的
1. func.caller严格模式不支持 (func函数的名字
1. 严格模式下,变量赋值前必须要声明——>之前未声明前就赋值,该变量是全局的
1. 局部this必须被赋值,否则为undefined (Person.call(null | undefined)中call里面是什么,this就是什么)
1. 拒绝重复属性和参数:参数—>function t(name,name){ } 属性:var obj={ name:”1”,name:”2” }

image.png

  1. "use strict"//全局
  2. function test(){
  3. //"use strict"局部
  4. console.log(arguments.callee);
  5. }
  6. test();
  7. //1.严格模式不支持with
  8. var obj = {
  9. name: "obj"
  10. }
  11. var name = "window"
  12. function test() {
  13. var name = 'scope';
  14. // with可以改变作用域链,会把with括号里面的参数变成作用域的最顶端obj = {name: "obj" }
  15. with(obj) {
  16. console.log(name);
  17. }
  18. }
  19. test();//obj
  20. //2.严格模式局部this必须被赋值
  21. "use strict"
  22. function test(){
  23. console.log(this);
  24. }
  25. test();// undefined
  26. new test();// >test{}
  27. test.call({})// {}

image.png