1、自定义Array.prototype.unshift
    (1)利用splice,改变原数组

    1. Array.prototype.unshift = function(){
    2. for(var position = 0;position < arguments.length; position++){
    3. this.splice(position, 0, arguments[position])
    4. }
    5. return this.length
    6. }

    (2)利用concat,不改变原数组,返回新数组。利用Array.prototype.slice.call(arguments)将类数组转换成数组

    1. Array.prototype.myUnshift = function(){
    2. for(var i = 0; i < arguments.length; i++){
    3. //this = [arguments[i]].concat(this) => 函数运行期间不能给this赋值,否则会报错。
    4. }
    5. }
    6. Array.prototype.myUnshift = function(){
    7. let arr = Array.prototype.slice.call(arguments);
    8. //当有多个参数时并包含concat时,参数数组会被‘扁平化’,
    9. //这个是concat的特性导致(参数为数组时,合并数组,为其他值时,将参数push到数组末尾)
    10. let newArr = this.concat(arr)
    11. return newArr
    12. }

    2、根据每个元素的总字节数,对数组进行排序

    1. function sumStrBytes(str){
    2. let sumBytes = str.length
    3. for(var i = 0; i <str.length; i++){
    4. if(str[i].charCodeAt() > 255) sumBytes++
    5. }
    6. return sumBytes
    7. }
    8. function sortByStrBytes(arr){
    9. arr.sort((a,b)=>{
    10. return sumStrBytes(a) - sumStrBytes(b)
    11. })
    12. }

    3、封装更精确的typeof方法

    1. //先判断是否为null
    2. //再根据typeof返回结果来区别引用值和原始值
    3. function myTypeof(val){
    4. var toString = Object.prototype.toString,
    5. resOfToString = toString.call(val),
    6. resOfTypeof = typeof(val),
    7. exactTypeMatch = {
    8. '[object Objec]' : 'object',
    9. '[object Function]' : 'function',
    10. '[object Array]' : 'array',
    11. '[object Number]' : 'object number',
    12. '[object String]' : 'object string',
    13. '[object Boolean]' : 'object boolean'
    14. }
    15. if(val === null){
    16. return 'null'
    17. }else if(resOfTypeof === 'object'){
    18. return exactTypeMatch[resOfToString]
    19. }else{
    20. return resOfTypeof
    21. }
    22. }

    4、数组去重
    利用对象属性名唯一的特点

    1. Array.prototype.unnique = function(arr){
    2. let temArr = [],
    3. obj = {};
    4. for(var i = 0; i < this.length; i++){
    5. if(!this[i]){
    6. obj[this[i]] = true //赋一个正值,给之后判断留一个标记,
    7. //非得直接将元素作为属性名的话,可以用hasOwnProperty来判断,是否已经赋值了
    8. temArr.push(obj[this[i]])
    9. }
    10. }
    11. return temArr
    12. }
    13. //用forEach和Object.values方法
    14. Array.prototype.unique = function(arr){
    15. let obj = {};
    16. this.forEach((item)=>{
    17. if(!obj[item]){
    18. obj[item] = item
    19. }
    20. })
    21. console.log(Object.values(obj))
    22. return Object.values(obj)
    23. }

    5、找出字符串中第一个只出现一次的字符
    思路,循环字符串,给每个字符记录出现次数。记录后,循环记录对象,找出第一个属性值为1的属性。

    1. let str = 'eqwerqwerqewrrasdfej';
    2. function recordStrAmount(arr){
    3. let obj = {};
    4. for(var i = 0; i<arr.length; i++){
    5. if(obj.hasOwnProperty(arr[i])){
    6. obj[arr[i]]++
    7. }else{
    8. obj[arr[i]] = 1
    9. }
    10. }
    11. //得到字符出现次数
    12. for(var key in obj){
    13. if(obj[key] === 1){
    14. return key
    15. }
    16. }
    17. }

    6、一些面试题
    (1)函数表达式与typeof

    1. let fn = funciont myFn(){
    2. }
    3. console.log(typeof(myFn)) // 返回undefined,typeof(undefined 和 为定义的变量)
    4. //myFn()报错,因为函数表达式会忽略函数名,
    5. //在外部是访问不到该函数的,但是在函数内部可以自调用

    (2)利用逗号制造稀松数组来适应取值

    1. let arr=['一','二','三'];
    2. //参数传阿拉伯数字,打印对应大写汉字
    3. //如果不将传入的数字减一,可以操作数组
    4. arr=[,'一','二','三']