用php的方式写javascript之01

179 阅读1分钟

一个后端php去写前端mvc是经常的事,但是好不习惯要去学习很多js的函数,还是习惯用php的方式处理。 今天分享一下首次的一个js脚本,函数名称和php是一样的,实现的功能也是一样的。 来,今天分享第一部分: ` /** * 用php的方式玩转js——01 */

//判断是否是整型
function is_int(i) {
    return i === +i && isFinite(i) && !(i % 1)
}

//判断是否是浮点类型
function is_float(i) {
    return !(+i !== i || isFinite(i) && !(i % 1))
}

//判断是否是数组
function is_array(t) {
    if (!t || "object" != typeof t) return !1;
    if (function (t) {
        if (!t || "object" != typeof t || "number" != typeof t.length) return !1;
        var e = t.length;
        return t[t.length] = "bogus", e !== t.length ? (t.length -= 1, !0) : (delete t[t.length], !1)
    }(t)) return !0;
    var e = Object.prototype.toString.call(t), n = function (t) {
        var e = /\W*function\s+([\w$]+)\s*\(/.exec(t);
        return e ? e[1] : "(Anonymous)"
    }(t.constructor);
    return "[object Object]" === e && "Object" === n
}

//判断是否是对象
function is_object(t) {
    return "[object Array]" !== Object.prototype.toString.call(t) && (null !== t && "object" == typeof t)
}

//判断函数是否存在
function function_exists(n) {
    var o = "undefined" != typeof window ? window : global;
    return "string" == typeof n && (n = o[n]), "function" == typeof n
}

//获取序列
function uniqid(n, e) {
    void 0 === n && (n = "");
    var t, i = function (n, e) {
        return n = parseInt(n, 10).toString(16), e < n.length ? n.slice(n.length - e) : e > n.length ? Array(e - n.length + 1).join("0") + n : n
    }, o = "undefined" != typeof window ? window : global;
    o.$locutus = o.$locutus || {};
    var d = o.$locutus;
    return d.php = d.php || {}, d.php.uniqidSeed || (d.php.uniqidSeed = Math.floor(123456789 * Math.random())), d.php.uniqidSeed++, t = n, t += i(parseInt((new Date).getTime() / 1e3, 10), 8), t += i(d.php.uniqidSeed, 5), e && (t += (10 * Math.random()).toFixed(8).toString()), t
}

//设置cookie
function setcookie(c, e, g) {
    if (e === undefined) {
        var f = "; " + document.cookie;
        var d = f.split("; " + c + "=");
        if (d.length === 2) {
            return d.pop().split(";").shift()
        }
        return null
    } else {
        if (e === false) {
            g = -1
        }
        var a = "";
        if (g) {
            var b = new Date();
            b.setTime(b.getTime() + (g * 24 * 60 * 60 * 1000));
            a = "; expires=" + b.toGMTString()
        }
        document.cookie = c + "=" + e + a + "; path=/"
    }
};

//判断变量是否为空
function empty(r) {
    var n, t, e, f = [void 0, null, !1, 0, "", "0"];
    for (t = 0, e = f.length; t < e; t++) if (r === f[t]) return !0;
    if ("object" == typeof r) {
        for (n in r) if (r.hasOwnProperty(n)) return !1;
        return !0
    }
    return !1
}

//判断是否已经定义了变量
function isset() {
    var r = arguments, t = r.length, n = 0;
    if (0 === t) throw new Error("Empty isset");
    for (; n !== t;) {
        if (void 0 === r[n] || null === r[n]) return !1;
        n++
    }
    return !0
}

//转换为整型
function intval(i, t) {
    var n, a, e = typeof i;
    return "boolean" === e ? +i : "string" === e ? (0 === t && (a = i.match(/^\s*0(x?)/i), t = a ? a[1] ? 16 : 8 : 10), n = parseInt(i, t || 10), isNaN(n) || !isFinite(n) ? 0 : n) : "number" === e && isFinite(i) ? i < 0 ? Math.ceil(i) : Math.floor(i) : 0
}

//转换为浮点类型
function floatval(a) {
    return parseFloat(a) || 0
}`