1.1 在原型上重写unshift

  1. var arr = ['d', 'e', 'f'];
  2. Array.prototype.myUnshift = function(){
  3. var pos = 0,
  4. len = arguments.length;
  5. for(var i = 0; i < len; i++){
  6. this.splice(pos, 0, arguments[i]);
  7. pos ++;
  8. }
  9. return this.length;
  10. }
  11. arr.myUnshift('a', 'b', 'c');
  12. console.log(arr);//['a', 'b', 'c', 'd', 'e', 'f']

思路,利用splice,第一个参数是下标位置,每次循环加1,因为是添加,第二个参数填0就是不截取,每次循环把第三个值重新添加进去.

重点,arguments是function的实参列表,其实是类数组,可以利用Array.prototype.slice.call(arguments)变成数组,如

  1. function test(){
  2. console.log(arguments);//[1, 2, 3, 4, callee: ƒ, Symbol(Symbol.iterator): ƒ]
  3. console.log(Array.prototype.slice.call(arguments));//[1, 2, 3, 4]
  4. }
  5. test(1, 2, 3, 4);

第二种方法,利用concat

  1. var arr2 = ['d', 'e', 'f'];
  2. Array.prototype.myUnshift2 = function(){
  3. var argArr = Array.prototype.slice.call(arguments);
  4. return argArr.concat(this);
  5. }
  6. var newArray = arr2.myUnshift2('a', 'b', 'c');
  7. console.log(newArray);

1.2数组按照元素的字节数排序

  1. function getBytes(str){
  2. var bytes = str.length;//先让所有的元素算一个字节
  3. for(var i = 0; i < str.length; i++){
  4. if(str.charCodeAt(i) > 255){//注意,unicode 0-255是算1个字节,256-之后算2个字节
  5. bytes++;
  6. }
  7. }
  8. return bytes;
  9. }
  10. console.log(getBytes('我爱你'));//6
  11. var arr3 = ['我爱你', 'OK', 'Hello', '你说WHAT', '可以'];
  12. arr3.sort(function(a, b){
  13. return getBytes(a) - getBytes(b);
  14. })
  15. console.log(arr3);//['OK', '可以', 'Hello', '我爱你', '你说WHAT']

2.1封装typeof

思路,typeof能返回的值 number boolean object function undefined,null返回的是object

  1. function myTypeof(val){
  2. var type = typeof(val),
  3. toStr = Object.prototype.toString,
  4. res = {
  5. '[object Array]': 'array',
  6. '[object Object]': 'object',
  7. '[object Number]': 'object number',
  8. '[object String]': 'object string',
  9. '[object Boolean]': 'object boolean'
  10. };
  11. if(val === null){
  12. return 'null';
  13. }else if(type === 'object'){
  14. var ret = toStr.call(val);
  15. return res[ret];
  16. }else{
  17. return type;
  18. }
  19. }
  20. console.log(myTypeof());

2.2数组去重

思路,利用对象键名的不重复性

  1. var arr4 = [0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 'a', 'a'];
  2. Array.prototype.unique = function(){
  3. var temp = {},
  4. newArr = [];
  5. for(var i = 0;i < this.length; i++){
  6. if(!temp.hasOwnProperty(this[i])){//注意,如果写成!temp[this[i]],!0就是true,所以0过滤不掉
  7. temp[this[i]] = this[i];
  8. newArr.push(this[i]);
  9. }
  10. }
  11. return newArr;
  12. }
  13. console.log(arr4.unique());

2.3字符串去重

同理

  1. var str = '0000111122222546545644445556aaabbc';
  2. String.prototype.unique2 = function(){
  3. var temp = {},
  4. newStr = '';
  5. for(var i = 0;i < this.length; i++){
  6. if(!temp.hasOwnProperty(this[i])){
  7. temp[this[i]] = this[i];
  8. newStr += this[i];
  9. }
  10. }
  11. return newStr;
  12. }
  13. console.log(str.unique2());

2.4面试题,找出第一个不重复的值

思路,还是同样的,让对象的valua值计数,出现几次加几次,最后遍历,返回打印第一个键名

  1. var str2 = 'wiiiieeeewrrtppaettttbfjgldyyuuuuuuk';
  2. function unique3(str){
  3. var temp = {};
  4. for(var i = 0; i < str.length; i++){
  5. if(temp.hasOwnProperty(str[i])){
  6. temp[str[i]]++;
  7. }else{
  8. temp[str[i]] = 1;
  9. }
  10. }
  11. for(var key in temp){
  12. if(temp[key] === 1){
  13. return key;
  14. }
  15. }
  16. }
  17. console.log(unique3(str2));//a

3.1面试题一,闭包

  1. function Test(a, b, c){
  2. var d = 0;
  3. this.a = a;
  4. this.b = b;
  5. this.c = c;
  6. function e(){
  7. d++;
  8. console.log(d);
  9. }
  10. this.f = e;
  11. }
  12. var test1 = new Test();
  13. test1.f();//1
  14. test1.f();//2
  15. var test2 = new Test();
  16. test2.f();//1

3.2面试题二

  1. function test3(){
  2. console.log(typeof(arguments));//object
  3. }
  4. test3();

3.3面试题三

  1. var test4 = function a(){
  2. return 'a';
  3. }
  4. console.log(typeof(a));//undefined,函数表达式是忽略函数名的,typeof里放一个未被声明的变量就是undefined
  5. var test5 = function a(){
  6. console.log(test5.name);//a
  7. }
  8. test5();

面试题四,如何优化这个函数

  1. function test6(day){
  2. switch(day){
  3. case 1:
  4. console.log('Mon');
  5. break;
  6. case 2:
  7. console.log('Tue');
  8. break;
  9. case 3:
  10. console.log('Wed');
  11. break;
  12. case 4:
  13. console.log('Thu');
  14. break;
  15. case 5:
  16. console.log('Fri');
  17. break;
  18. case 6:
  19. console.log('Sat');
  20. break;
  21. case 7:
  22. console.log('Sun');
  23. break;
  24. default:
  25. console.log('I don\'t know');
  26. }
  27. }
  28. test6(4);//Thu

优化

  1. function test7(day){
  2. var weekday = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
  3. return weekday[day - 1] !== undefined ? console.log(weekday[day - 1]) : console.log('I don\'t know');
  4. }
  5. test7(0);