美文网首页
JS常用方法(函数)总结

JS常用方法(函数)总结

作者: 稚儿擎瓜_细犬逐蝶 | 来源:发表于2020-04-14 18:10 被阅读0次
    • Node 获取本机 ip 地址

    const interfaces = require('os').networkInterfaces()
    let IPAddress = ''
    for (let devName in interfaces) {
        let iface = interfaces[devName]
        for (let i = 0; i < iface.length; i++) {
            let alias = iface[i]
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                IPAddress = alias.address
            }
        }
    }
    
    • 点击滚动到某个位置

    function scrollToSomeWhere(ele, speed){
        if(!speed) speed = 300;
        if(!ele){
            $("html,body").animate({scrollTop:0},speed);
        }else{
            if(ele.length>0) $("html,body").animate({scrollTop:$(ele).offset().top - 150},speed);
        }
        return false;
    }
    $('.contactUsBtn').click(function () {
        scrollToSomeWhere('#toContactUs',300)
    })
    
    • lodashmerge 函数实现

    function isObject (value) {
        const type = typeof value
        return value !== null && (type === 'object' || type === 'function')
    }
    function merge (source, other) {
       if (!this.isObject(source) || !this.isObject(other)) {
              return other === undefined ? source : other
        }
    // 合并两个对象的 key,另外要区分数组的初始值为 []
     return Object.keys({...source, ...other}).reduce((acc, key) => {
            // 递归合并 value
            acc[key] = this.merge(source[key], other[key])
            return acc
        }, Array.isArray(source) ? [] : {})
    },
    
    • jsObject.Assign方法的实现
    function zyEs6AssignPolyfill() {
           if (!Object.assign) {
               Object.defineProperty(Object, "assign", {
                   enumerable: false,
                   configurable: true,
                   writable: true,
                   value: function (target, firstSource) {
                       "use strict";
                       if (target === undefined || target === null)
                           throw new TypeError("Cannot convert first argument to object");
                       var to = Object(target);
                       for (var i = 1; i < arguments.length; i++) {
                           var nextSource = arguments[i];
                           if (nextSource === undefined || nextSource === null) continue;
                           var keysArray = Object.keys(Object(nextSource));
                           for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
                               var nextKey = keysArray[nextIndex];
                               var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
                               if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey];
                           }
                       }
                       return to;
                   }
               });
           }
       }
    

    相关文章

      网友评论

          本文标题:JS常用方法(函数)总结

          本文链接:https://www.haomeiwen.com/subject/qpiheqtx.html