资讯专栏INFORMATION COLUMN

JavaScript常用脚本集锦6

Acceml / 1174人阅读

摘要:它会指出一个类是继承自另一个类的。测试测试代码来源页面倒计时的一段运用倒计时的一段脚本。截止日期符合日期格式,比如等有效日期。截止的天数小时分钟秒数组成的对象。

清楚节点内的空格
function cleanWhitespace(element) {
    //如果不提供参数,则处理整个HTML文档
    element = element || document;
    //使用第一个节点作为开始指针
    var cur = element.firstChild;
    //一直循环,直到没有子节点为止。
    while (cur != null) {
        //如果节点是文本节点,并且只包含空格
        if ((cur.nodeType == 3) && !/S/.test(cur.nodeValue)) {
            element.removeChild(cur);
        }
        //一个节点元素
        else if (cur.nodeType == 1) {
            //递归整个文档
            cleanWhitespace(cur);
        }
        cur = cur.nextSibling;  //遍历子节点
    }
}

代码来源:https://gist.github.com/hehongwei44/9105deee7b9bde88463b

JavaScript中的类继承实现方式
/**
 * 把一个实例方法添加到一个类中
 * 这个将会添加一个公共方法到 Function.prototype中,
 * 这样通过类扩展所有的函数都可以用它了。它要一个名称和一个函数作为参数。
 * 它返回 this。当我写一个没有返回值的方法时,我通常都会让它返回this。
 * 这样可以形成链式语句。
 *
 * */
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
/**
 * 它会指出一个类是继承自另一个类的。
 * 它必须在两个类都定义完了之后才能定义,但要在方法继承之前调用。
 *
 * */
Function.method("inherits", function (parent) {
    var d = 0, p = (this.prototype = new parent());

    this.method("uber", function uber(name) {
        var f, r, t = d, v = parent.prototype;
        if (t) {
            while (t) {
                v = v.constructor.prototype;
                t -= 1;
            }
            f = v[name];
        } else {
            f = p[name];
            if (f == this[name]) {
                f = v[name];
            }
        }
        d += 1;
        r = f.apply(this, Array.prototype.slice.apply(arguments, [1]));
        d -= 1;
        return r;
    });
    return this;
});
/**
 *
 * The swiss方法对每个参数进行循环。每个名称,
 * 它都将parent的原型中的成员复制下来到新的类的prototype中
 *
 * */
Function.method("swiss", function (parent) {
    for (var i = 1; i < arguments.length; i += 1) {
        var name = arguments[i];
        this.prototype[name] = parent.prototype[name];
    }
    return this;
});

代码来源:https://gist.github.com/hehongwei44/2f89e61a0e6d4fd722c4

将单个字符串的首字母大写
/**
*
* 将单个字符串的首字母大写
* 
*/
var fistLetterUpper = function(str) {
        return str.charAt(0).toUpperCase()+str.slice(1);
};
console.log(fistLetterUpper("hello"));      //Hello
console.log(fistLetterUpper("good"));       //Good

代码来源:https://gist.github.com/hehongwei44/7e879f795226316c260d

变量的类型检查方式
/**
*
* js的类型检测方式->typeof、constuctor。
* 推荐通过构造函数来检测变量的类型。
*/
var obj = {key:"value"},
        arr = ["hello","javascript"],
        fn  = function(){},
        str = "hello js",
        num = 55,
        bool = true,
        User = function(){},
        user = new User();
    /*typeof测试*/
    console.log(typeof obj);    //obj
    console.log(typeof arr);    //obj
    console.log(typeof fn);     //function
    console.log(typeof str);    //string
    console.log(typeof num);    //number
    console.log(typeof bool);   //boolean
    console.log(typeof user);   //object
    /*constructor测试*/
    console.log(obj.constructor == Object); //true
    console.log(arr.constructor == Array);  //true
    console.log(str.constructor == String); //true
    console.log(num.constructor == Number); //true
    console.log(bool.constructor == Boolean);//true
    console.log(user.constructor == User);  //true

代码来源:https://gist.github.com/hehongwei44/1d808ca9b7c67745f689

页面倒计时的一段运用
/**
*
* @descition: 倒计时的一段脚本。
* @param:deadline ->截止日期 符合日期格式,比如2012-2-1 2012/2/1等有效日期。
* @return -> 截止的天数、小时、分钟、秒数组成的object对象。
*/
function getCountDown(deadline) {
        var activeDateObj = {},
             currentDate  = new Date().getTime(),            //获取当前的时间
             finalDate    = new Date(deadline).getTime(),    //获取截止日期
             intervaltime = finalDate - currentDate;         //有效期时间戳

        /*截止日期到期的话,则不执行下面的逻辑*/
        if(intervaltime < 0) {
            return;
        }

        var totalSecond = ~~(intervaltime / 1000),     //得到秒数
            toDay       = ~~(totalSecond / 86400 ),   //得到天数
        toHour      = ~~((totalSecond -  toDay * 86400) / 3600), //得到小时
        tominute    = ~~((totalSecond -  toDay * 86400 - toHour * 3600) / 60), //得到分数
        toSeconde   = ~~(totalSecond - toDay * 86400 - toHour * 3600 -tominute * 60);

    /*装配obj*/
    activeDateObj.day    = toDay;
    activeDateObj.hour   = toHour;
    activeDateObj.minute = tominute;
    activeDateObj.second = toSeconde;

    return activeDateObj;
}

代码来源:https://gist.github.com/hehongwei44/a1205e9ff17cfc2359ca

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/87691.html

相关文章

  • JavaScript常用脚本集锦2

    摘要:把中的伪数组转换为真数组在中,函数中的隐藏变量和用获得的元素集合都不是真正的数组,不能使用等方法,在有这种需要的时候只能先转换为真正的数组。检测元素是否支持某个属性代码用法创建和使用命名空间使用方式 把JavaScript中的伪数组转换为真数组 在 JavaScript 中, 函数中的隐藏变量 arguments 和用 getElementsByTagName 获得的元素集合(Nod...

    xialong 评论0 收藏0
  • JavaScript常用脚本集锦5

    摘要:代码来源一些常用的操作方法介绍查找相关元素的前一个兄弟元素的方法。查找元素指定层级的父元素。 DOM操作的增强版功能函数 /** * 将一个DOM节点、HTML字符串混合型参数 * 转化为原生的DOM节点数组 * * */ function checkElem(a) { var r = []; if (a.constructor != Array) { ...

    joywek 评论0 收藏0
  • JavaScript常用脚本集锦7

    摘要:将加法和加上校验位能被整除。下面分别分析出生日期和校验位检查生日日期是否正确输入的身份证号里出生日期不对将位身份证转成位校验位按照的规定生成,可以认为是数字。校验位按照的规定生成,可以认为是数字。表示全部为中文为不全是中文,或没有中文。 判断是否是合理的银行卡卡号 //Description: 银行卡号Luhm校验 //Luhm校验规则:16位银行卡号(19位通用): // 1.将...

    lindroid 评论0 收藏0
  • JavaScript常用脚本集锦1

    摘要:初始化参数可选参数,必填参数可选,只有在请求时需要参数可选回调函数可选参数可选,默认为参数可选,默认为创建引擎对象打开发送普通文本接收文档将字符串转换为对象最后,说明一下此函数的用法。即等待与成功回调,后标志位置为。 jquery限制文本框只能输入数字 jquery限制文本框只能输入数字,兼容IE、chrome、FF(表现效果不一样),示例代码如下: $(input).keyup(...

    ygyooo 评论0 收藏0
  • JavaScript常用脚本集锦3

    通过数组,拓展字符串拼接容易导致性能的问题 function StringBuffer() { this.__strings__ = new Array(); } StringBuffer.prototype.append = function (str) { this.__strings__.push(str); return this; } StringBuffer....

    dack 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<