一、封装myUnshift方法
1.1、利用循环
// 封装myUnshift方法// 方法一Array.prototype.myUnshift = function(){var pos = 0;for(var i = 0; i < arguments.length; i++){this.splice(pos, 0, arguments[i]);pos++;}return this.length;}var arr = [3, 4, 5];arr.myUnshift(1, 2);console.log(arr);
1.2、把类数组转换为数组,然后借用数组拼接的方法
// 封装myUnshift方法// 方法二: 把类数组转换为数组,然后借用数组拼接的方法Array.prototype.myUnshift = function(){var argArr = Array.prototype.slice.call(arguments);var newArr = argArr.concat(this);return newArr;}var arr = [3, 4, 5];console.log(arr.myUnshift(1, 2));
二、数组元素按照字节数排序
unicode 0~255 是一个字节;255~是两个字节 ```javascript
// 数组元素按照字节数排序// unicode 0~255 是一个字节;255~是两个字节function getBytes(str){var bytes = str.length;for(var i = 0; i < str.length; i++){if(str.charCodeAt(i) > 255){bytes++;}}return bytes;}var arr = ['我爱你', 'OK', 'Hello', '你说WHAT'];arr.sort(function(a, b){return getBytes(a) - getBytes(b);});console.log(arr)
<a name="FsRhe"></a># 三、封装myTypeof方法- typeof返回的结果:'number' / 'string' / 'boolean' / 'undefined' / 'object' / 'function'- 万能数据类型检测:Object.prototype.toString.call```javascript// 封装myTypeof方法// typeof返回的结果:'number' / 'string' / 'boolean' / 'undefined' / 'object' / 'function'// 万能数据类型检测:Object.prototype.toString.callfunction myTypeof(val){var type = typeof(val),toStr = Object.prototype.toString,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;}}console.log(myTypeof(null));console.log(myTypeof(undefined));console.log(myTypeof(new Number(1)));console.log(myTypeof([]));console.log(myTypeof(function(){}));
四、封装一个数组去重的方法
- 新建一个空对象
- 新建一个空数组
循环要去重的数组,利用对象属性名不能重复的特性,来实现数组去重
// 封装一个数组去重的方法// 新建一个空对象// 新建一个空数组// 利用对象属性名不能重复的特性,来实现数组去重Array.prototype.unique = function(){var temp = {},newArr = [];for(var i = 0; i < this.length; i++){if(!temp.hasOwnProperty(this[i])){temp[this[i]] = this[i];newArr.push(this[i]);}}return newArr}var arr = [1,1,1,2,2,3,4];var res = arr.unique();console.log(res);
五、封装一个方法实现字符串去重
// 封装一个方法实现字符串去重String.prototype.unique = function(){var temp = {},newStr = '';for(var i = 0; i < this.length; i++){if(!temp.hasOwnProperty(this[i])){temp[this[i]] = this[i];newStr += this[i];}}return newStr}var str = 'nnnooooolllllltttlyyy';console.log(str.unique(str));
六、百度外卖面试题
找到第一次出现不重复的字母
// 找到第一次出现不重复的字母var str = 'ajahfdjsabljdfbalgfq';function firstAppear(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;}}}var res = firstAppear(str);console.log(res);
七、优化下面程序
```javascript
// 优化下面程序function test(day){switch(day){case 1:console.log('周一');break;case 2:console.log('周二');break;case 3:console.log('周三');break;case 4:console.log('周四');break;case 5:console.log('周五');break;case 6:console.log('周六');break;case 7:console.log('周日');break;default:console.log('我不知道');}}test(1);test(9);// 数组边界的判断var arr = [1,2,3,4];console.log(arr[4]);// 利用数组来优化function week(day){var weekArr = ['周一','周二','周三','周四','周五','周六','周日'];weekArr[day-1] !== undefined ?console.log(weekArr[day-1]):console.log('我不知道');}week(1);week(6);week(9);// 现在我不想用day-1的形式,想传入什么就数组星期几,利用数组的特性应该怎么做?// 稀松数组:数组不一定每一项都需要值function week(day){var weekArr = [,'周一','周二','周三','周四','周五','周六','周日'];weekArr[day] !== undefined ?console.log(weekArr[day]):console.log('我不知道');}week(1);week(6);week(9);
```
