| 学习时间 |
完成时间 ✅ |
重难点 |
疑问/遗漏 |
|
|
|
|
| 复习时间 |
|
|
|
|
|
|
封装typeof方法
typeof数据类型判断会返回的值:number string boolean object function undefined 且结果都是字符串类型function myTypeof(val){ var type=typeof(val); var toStr=Object.prototype.toString; var res={ '[object Array]':'array', '[object Object]': 'object', '[object Number]': 'object number', '[object String]':'object string', '[object Boolean]': 'object boolean' } if(val===null){ return 'null'; }else if(type==='object'){ var ret=toStr.call(val); return res[ret] }else{ return type; }}
数组去重
解析:因为对于对象不可能存在两个相同完全相同的属性借用key值不会重复,给数组去重{ name:'ckl', name:';;;' //此情景不会存在两个相同的属性}var arr=[1,4,2,14,2,1,2]Array.prototype.unique=function(){ var temp={}; newArr=[]; for(var i=0;i<this.length;i++){ if(!temp[this[i]]){ //此处temp[0]=0是个特殊的值 // temp[this[i]]=this[i]; temp[this[i]]='value'; //这个值要为非0的值 newArr.push(this[i]) } //或者 判断的方式二 // if(!temp.hasOwnProperty(this[i])){ // temp[this[i]]=this[i]; // newArr.push(this[i]) // } } return newArr;}console.log(arr.unique())知识补充var obj={ name:'123'}console.log(obj.hasOwnProperty('name')) //trueconsole.log(obj.hasOwnProperty('kk')) //false obj没有kk属性
字符串的去重
var str='112314357392848'String.prototype.unique=function(){ var temp={}; var newStr=''; for(var i=0;i<this.length;i++){ if(!temp.hasOwnProperty(this[i])){ temp[this[i]]=this[i]; newStr+=this[i] } } return newStr;}console.log(str.unique())
字符串中第一个非重复字符
var str='avbgsasdsanssj';function test(str){ var temp={}; for(var i=0;i<str.length;i++){ if(temp.hasOwnProperty(str[i])){ temp[str[i]]++; }else{ temp[str[i]]=1; } } for(var key in temp){ if(temp[key]===1){ return key; } }}console.log(test(str))
闭包练习
function Test(a,b,c){ var d=0; this.a=a; this.b=b; this.c=c; function e(){ d++; console.log(d); } this.f=e;}var test1=new Test();test1.f();//1test1.f();//2var test2=new Test();test2.f();//1
练习题
第一题function test(){ console.log(typeof(arguments)); //object}test()第二题var test=function a(){ return 'a';}console.log(typeof(a))//undefined 因为函数表达式忽略函数名字 a正常情况下无需定义console.log(a) //报错 因为a未定义