常用的js方法

栏目: jQuery · 发布时间: 6年前

内容简介:在易企秀工作了快两年了,做过不同的项目,接触过不同的框架,发现有许多基础的方法使用率很高,今天简单的总结一部分(不全,以后慢慢补吧)。1,通过 constructor 判断2,通过instanceof 判断判断

在易企秀工作了快两年了,做过不同的项目,接触过不同的框架,发现有许多基础的方法使用率很高,今天简单的总结一部分(不全,以后慢慢补吧)。

一,判断当前元素是否是数组

1,通过 constructor 判断

function isArray(value) {
    return value && typeof value == 'object' && value.constructor === Array
}
复制代码

2,通过instanceof 判断判断

function isArray(value) {
    return value && typeof value == 'object' && value instanceof Array
}
复制代码

3,通过 toString 判断

function isArray(value) {
    return Array.isArray(value) || (typeof value == 'object' && Object.prototype.toString.call(value) === '[object Array]')
}
复制代码

二,判断是否是对象

function isObject(value) {
    return value != null && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]'
}
复制代码

三,判断浏览器环境

1,判断是否安卓

function isAndroid() {
    return /Android/i.test(navigator.userAgent) || /Linux/i.test(navigator.appVersion);
}
复制代码

2,判断是否ios

function isIOS() {
    return (/ipad|iphone/i.test(navigator.userAgent));
}
复制代码

3,判断是否是Safari

function isSafari() {
    return (/msie|applewebkit.+safari/i.test(navigator.userAgent));
}
复制代码

4,判断是否在微信

function isWeixin() {
    return /MicroMessenger/i.test(navigator.userAgent);
}
复制代码

三,使用promise封装ajax(对jq的ajax的封装)

1,普通的封装

function $ajax(config) {
    return new Promise(function (resolve, reject) {
        $.ajax($.extend({}, config, {
            success: function (data) {
                if (data && data.success === false) {
                    reject(data);
                } else {
                    resolve(data);
                }
            },
            error: function (...args) {
                console.error(config, ...args);
                reject(...args);
            }
        }));
    });
}
复制代码

2,添加跨域的ajax

function ajax(config) {
    return new Promise(function (resolve, reject) {
        $.ajax($.extend(
            {
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true
            },
            config,
            {
                success: function (data) {
                    if (data && data.success === false) {
                        reject(data);
                    } else {
                        resolve(data);
                    }
                },
                error: function (...args) {
                    console.error(config, ...args);
                    reject(...args);
                }
            }));
    });
}
复制代码

对于ajax请求,我们可能直接将后续的一些业务逻辑直接写在了ajax的会调里,如果业务逻辑比较复杂,就会造成代码嵌套层级较深,不好阅读与维护。这里我们用promise对ajax进行简单的封装,这样我们将后续的业务写在then()里,可以避免‘回调地狱’的产生。

四,对象的深拷贝

1,对于object

// 简单粗暴,一步到位
JSON.parse(JSON.stringify(obj));
复制代码

2,对于数组,我们可以用Array.slice(),Array.concat(),ES6扩展运算符...来实现。

以上大概是目前来说运用的最多的一些公用方法,可能实现方法不是最好的,这里仅供参考。还有许多通用的方法,这里只想起这么多了,以后再慢慢补充吧。


以上所述就是小编给大家介绍的《常用的js方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Operating System Algorithms

Operating System Algorithms

Nathan Adams、Elisha Chirchir / CreateSpace Independent Publishing Platform / 2017-4-21 / USD 39.15

Operating System Algorithms will walk you through in depth examples of algorithms that you would find in an operating system. Selected algorithms include process and disk scheduling.一起来看看 《Operating System Algorithms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具