1.1 在原型上重写unshift
var arr = ['d', 'e', 'f'];
Array.prototype.myUnshift = function(){
var pos = 0,
len = arguments.length;
for(var i = 0; i < len; i++){
this.splice(pos, 0, arguments[i]);
pos ++;
}
return this.length;
}
arr.myUnshift('a', 'b', 'c');
console.log(arr);//['a', 'b', 'c', 'd', 'e', 'f']
思路,利用splice,第一个参数是下标位置,每次循环加1,因为是添加,第二个参数填0就是不截取,每次循环把第三个值重新添加进去.
重点,arguments是function的实参列表,其实是类数组,可以利用Array.prototype.slice.call(arguments)变成数组,如
function test(){
console.log(arguments);//[1, 2, 3, 4, callee: ƒ, Symbol(Symbol.iterator): ƒ]
console.log(Array.prototype.slice.call(arguments));//[1, 2, 3, 4]
}
test(1, 2, 3, 4);
第二种方法,利用concat
var arr2 = ['d', 'e', 'f'];
Array.prototype.myUnshift2 = function(){
var argArr = Array.prototype.slice.call(arguments);
return argArr.concat(this);
}
var newArray = arr2.myUnshift2('a', 'b', 'c');
console.log(newArray);
1.2数组按照元素的字节数排序
function getBytes(str){
var bytes = str.length;//先让所有的元素算一个字节
for(var i = 0; i < str.length; i++){
if(str.charCodeAt(i) > 255){//注意,unicode 0-255是算1个字节,256-之后算2个字节
bytes++;
}
}
return bytes;
}
console.log(getBytes('我爱你'));//6
var arr3 = ['我爱你', 'OK', 'Hello', '你说WHAT', '可以'];
arr3.sort(function(a, b){
return getBytes(a) - getBytes(b);
})
console.log(arr3);//['OK', '可以', 'Hello', '我爱你', '你说WHAT']
2.1封装typeof
思路,typeof能返回的值 number boolean object function undefined,null返回的是object
function 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());
2.2数组去重
思路,利用对象键名的不重复性
var arr4 = [0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 'a', 'a'];
Array.prototype.unique = function(){
var temp = {},
newArr = [];
for(var i = 0;i < this.length; i++){
if(!temp.hasOwnProperty(this[i])){//注意,如果写成!temp[this[i]],!0就是true,所以0过滤不掉
temp[this[i]] = this[i];
newArr.push(this[i]);
}
}
return newArr;
}
console.log(arr4.unique());
2.3字符串去重
同理
var str = '0000111122222546545644445556aaabbc';
String.prototype.unique2 = 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;
}
console.log(str.unique2());
2.4面试题,找出第一个不重复的值
思路,还是同样的,让对象的valua值计数,出现几次加几次,最后遍历,返回打印第一个键名
var str2 = 'wiiiieeeewrrtppaettttbfjgldyyuuuuuuk';
function unique3(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(unique3(str2));//a
3.1面试题一,闭包
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();//1
test1.f();//2
var test2 = new Test();
test2.f();//1
3.2面试题二
function test3(){
console.log(typeof(arguments));//object
}
test3();
3.3面试题三
var test4 = function a(){
return 'a';
}
console.log(typeof(a));//undefined,函数表达式是忽略函数名的,typeof里放一个未被声明的变量就是undefined
var test5 = function a(){
console.log(test5.name);//a
}
test5();
面试题四,如何优化这个函数
function test6(day){
switch(day){
case 1:
console.log('Mon');
break;
case 2:
console.log('Tue');
break;
case 3:
console.log('Wed');
break;
case 4:
console.log('Thu');
break;
case 5:
console.log('Fri');
break;
case 6:
console.log('Sat');
break;
case 7:
console.log('Sun');
break;
default:
console.log('I don\'t know');
}
}
test6(4);//Thu
优化
function test7(day){
var weekday = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
return weekday[day - 1] !== undefined ? console.log(weekday[day - 1]) : console.log('I don\'t know');
}
test7(0);