一、数组
|
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.模拟push()方法var arr=[5,6];var array=[0,2,4]Array.prototype.push=function (){for(var i=0;i<arguments.length;i++){//刚开始不知道谁的长度,this是谁调用就是谁this[this.length]=arguments[i]}}arr.push(1,2,3)console.log(arr);//2.splice内部原理splice = function (pos) {pos += pos > 0 ? 0 : this.length}arr.splice(-1,1)//当截取的为负数值时,在内部会有一个转换,加上数组的长度,为截取开始位//3.sort排序原理var arr = [1, 5, 2, 0, 11];arr.sort(function (a, b) {//a-b升序 b-a降序return a - b})console.log(arr); //[0,1,2,5,11]//4.利用sort随机排序数组,每次返回数组的排序不同var arr1 = [1, 2, 3, 4, 5, 6, 7];arr1.sort(function (a, b) {return Math.random() - 0.5})
**sort**
//1.对象属性排序var a = {name: "小a",age: 20,like: "香蕉"}var b = {name: "小b",age: 10,like: "苹果"}var c = {name: "小c",age: 30,like: "葡萄"}var arr = [a, b, c]arr.sort(function (a, b) {return a.age - b.age})console.log(arr);//2.字节排序// 按字节长度排列数组var arr=['c的','a','da得到','ssss','水水水sderf']function bytesLength(str) {var num = str.length;for (var i = 0; i < str.length; i++) {// charCodeAt可计算ASCII码值,>255汉字(2字节)if (str.charCodeAt(i) > 255) {num++;}}return num;}arr.sort(function (a, b) {return bytesLength(a) - bytesLength(b)})//["a", "c的", "ssss", "da得到", "水水水sderf"]console.log(arr);
1.2 不改变原数组的方法
返回值为新数组,原数组没有变化
1. concat() —->拼接两个数组:arr.concat(arr2)—->arr和arr2都没有变化1. toString() —>把数组转为字符串1. slice(从第几位开始截取,截取到哪一位) —->返回截取到的新数组,拿一个变量接收,否则无意义(注意:slice(1)从第一位开始截取到最后 )1. jion("-") 把数组转为字符串,并按照jion参数进行拼接1. split()把字符串按照split的参数拆分成数组(字符串数组) |
||
|---|---|---|
1.3 类数组
| 类数组要求: 1. 类数组的属性名为数组的索引值 1. 类数组要有length属性—->一定要有 1. 类数组 "splice":Array.prototype.splice 方法后,变为数组形式[],不再是对象{}形式1. "push":Array.prototype.push手动为类数组添加 push()方法 1. 类数组既可以当作数组来用,也可以当作对象来用——>类数组没有数组的所有方法,需要自己手动添加 1. arguments 就是类数组 |
||
|---|---|---|
1.
2.
var obj={"0":"a","1":"b","2":"c","length":3,"push":Array.prototype.push,"splice":Array.prototype.splice}// 类数组的push()原理Array.prototype.push=function(target){// 如果为类数组,this变为objthis[this.length]=target;this.length++;}obj.push("d");console.log(obj);//类数组联系var obj={"2":"a","3":"b","length":2,// push决定在哪一位加东西,由length决定"push":Array.prototype.push}obj.push('c')obj.push('d')console.log(obj);
1.3 封装typeof方法
/*** 1.判断,如果为null则返回null* 2.判断是不是object,如果为object则判断是包装类还是Array或者Object*/function myType(target) {// 1. 判断模板var template={'[object Array]':'array','[object Object]':'object',// 因为原始值已经过滤,得到以下值为包装类new Number(111)等'[object Number]':'number---object','[object String]':'string---object','[object Boolean]':'boolean---object',}// 2. 判断是不是nullif (target == null) {return null;}// 2.在这一步已经过滤了原始值,因为Object.prototype.toString.call(123)--->[object Number]if (typeof (target) == "object") {var str=Object.prototype.toString.call(target);return template[str]}else{//3.原始值,直接返回return typeof(target)}}
1.4 数组去重
把数组当作对象的属性名,给这个属性随便赋一个值,因为对象的属性名是不会重复的,所以判断对象的属性值是不是空,为空,就把这个数组值push进新数组中
//把数组的值当作对象的属性名,对象的属性名是不会重复的,因此可实现去重var arraa = [1, 2, 3, 1, 2, 3]Array.prototype.unique = function () {var temp = {};// 返回的新数组var arr = [];for (var i = 0; i < this.length; i++) {// 如果为undefind-->对象中没这个值if (!temp[this[i]]) {// 这一步是需要的,当有值时才不会返回undefinedtemp[this[i]]="123"//temp[this[i]]=this[i]是不可以的,如果this[i]=0,不能去重arr.push(this[i])}}return arr;}
二、try…catch…/es5严格模式
2.1 try{ }catch(e){}
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” } |
||
|---|---|---|

"use strict"//全局function test(){//"use strict"局部console.log(arguments.callee);}test();//1.严格模式不支持withvar obj = {name: "obj"}var name = "window"function test() {var name = 'scope';// with可以改变作用域链,会把with括号里面的参数变成作用域的最顶端obj = {name: "obj" }with(obj) {console.log(name);}}test();//obj//2.严格模式局部this必须被赋值"use strict"function test(){console.log(this);}test();// undefinednew test();// >test{}test.call({})// {}





