JS笔记

JS很经典很基础的一段代码:
    var a = [-1, -1, 1, -3,-5,8, 11, 55,2, 9,-13];
    function f(s, e){
        var ret = [];
        for(var i in s) {
            ret.push(e(s[i]));
        }
    }
    var b = f(a, function(n){ return n>0 ? n : 0; });
    alert(b);
假设你想要创建一个包,它包含了一串函数,用来显示各种不同的预定义的警告信息。你可以如下实现:
jscript = function() { }
jscript.ui = function() { }
jscript.ui.alerts = new function() { }
jscript.ui.alerts.showErrorAlert = function() { }
执行这段代码后,你将有一个名叫jscript的对象,它是一个函数的引用。这个对象中,有一个叫ui的成员,它本身就是一个函数。然后
这个对象(ui)内部是另一个叫alerts的对象,它也是一个函数。它内部是另一个对象,叫showErrorAlert,它还是一个函数。
他们给我们一个 包 和 子包 的“外观”。事实上,你有一系列可以单独地实例化的对象。你可以这样做:
var v = new jscript.ui.alerts();
创建第一个包jscript.array
如何将一个数组的内容赋值到另一个数组?
jscript.aray.copyArray = function(inSrcArray, inDestArray) {
    var i;
    for(i = 0; i < inSrcArray.length; i++) {
        inDestArray.push(inSrcArray[i]);
    }
    return inDestArray;
}
怎样在一个数组中查找指定的元素?
jscript.array.finInArray = function(inArray, inValue) {
    var i;
    for(i = 0; i < inArray.length; i++) {
        if(inArray[i] == inValue) {
            return i;
        }
    }
    return -1;
}
如何计算数组中所有元素的平均值?
jscript.array.arrayAverage = function(inArray) {
    var accumulator = 0;
    var i;
    for(i = 0; i < inArray.length; i++) {
        accumulator += inArray[i];
    }
    return accumulator / inArray.length;
}
构建jscript.datetime包,怎样才能知道一个给定月有多少天?
jscript.datetime.getNumberDaysInMonth = function(inMonth, inYear) {
    inMonth = inMonth - 1;
    var leap_year = this.isLeapYear(inYear);
    if(leap_year) {
        leap_year = 1;
    } else {
        leap_year = 0;
    }
    if(inMonth == 3 || inMonth == 5 || inMonth == 8 || inMonth == 10) {
        return 30;
    } else if(inMonth == 1) {
        return 28 + leap_year;
    } else {
        return 31;
    }
}
jscript.datetime.isLeapYear = function(inYear) {
    if((inYear%4 == 0 && !(inYear%100 == 0)) || inYear%400 == 0) {
        return true;
    } else {
        return false;
    }
}
构建jscript.debug包,如何显示任意一个对象的所有属性以及它们的值?
jscript.debug.enumProps = function(inObj) {
    var props = "";
    var i;
    fo(i in inObj) {
        props += i + "=" + inObj[i] + "\n";
    }
    alert(props);
}
构建jscript.dom包,如何将任意的一个DOM元素居中?
jscript.dom.layerCenterH = function(inObj) {
    var lca;
    var lcb;
    var lcx;
    var iebody;
    var dsocleft;
    if(window.innerWidth) {
        lca = window.innerWidth;
    } else {
        lca = document.body.clientWidth;
    }
    lcb = inObj.offsetWidth;
    lcx = (Math.round(lca / 2)) - (Math.round(lcb / 2));
    iebody = (document.compatMode && document.compatMode != "BackCompat") ?
            document.documentElement : document.body;
    dsocleft = document.all ? iebody.scrollLeft : window.pageXOffset;
    inObj.style.left = lcx + dsocleft + "px";
}
当使用一个Ajax请求时,得到一堆返回的内容中包含<script>段,如何执行它们?
jscript.dom.execSripts = function(inText) {
    var si = 0;
    while(true) {
        //finding opening script tag.
        var ss = inText.indexOf("<" + "script" + ">", si);
        if(ss == -1) {
            return;
        }
        //finding closing script tag;
        var se = inText.indexOf("<" + "/" + "script" +">", ss);
        if(se == -1) {
            return;
        }
        //jump ahead 9 characters, after the closing script tag.
        si = se + 9;
        //get the content in between and execute it.
        var sc = inText.substring(ss + 8, se);
        eval(sc);
    }
}
构建jscript.form包,如何从一个HTML表单生成XML?
jscript.form.formToXML = function(inForm, inRootElement) {
    if(inForm == null) {
        return null;
    }
    if(inRootElement == null) {
        return null;
    }
    var outXML = "<" + inRootElement + ">";
    var i;
    for(i = 0; i < inForm.length; i++) {
        var ofe = inForm[i];
        var ofeType = ofe.type.toUpperCase();
        var ofeName = ofe.name;
        var ofeValue = ofe.value;
        if(ofeType == "TEXT" || ofeType == "HIDDEN" ||
            ofeType == "PASSWORD" || ofeType == "SELECT-ONE" ||
            ofeType == "TEXTAREA") {
               outXML += "<" + ofeName + ">" + ofeValue + "</" + ofeName + ">";
        }
        if(ofeType == "RADIO" && ofe.checked == true) {
            outXML += "<" + ofeName + ">" +ofeValue + "</" +ofeName + ">";
        }  
        if(ofeType == "CHECKBOX") {
            if(ofe.checked == true) {
                cbval = "true";
            } else {
                cbval = "false";
            }
            outXML = outXML + "<" +ofeName + ">" +cbval + "</" + ofeName + ">"; 
        }
        outXML += "";
    }
    outXML += "</" + inRootElement + ">";
    return outXML;
}
构建jscript.storage包,如何创建一个cookie并把它保存在客户端?
jscript.storage.setCookie = function(inName, inValue, inExpiry) {
    if(typeof inExpiry == "Date") {
        inExpiry = inExpiry.toGMTString();
    }
    document.cookie = inName + "=" + escape(inValue) + ";expires=" + inExpiry;
}
设置cookie后怎么取它的值呢?
jscript.storage.getCookie = function(inName) {
    var docCookies = document.cookie;
    var cIndex = docCookies.indexOf(inName + "=");
    if(cIndex == -1) {
        return null;
    }
    cIndex = docCookies.indexOf("=", cIndex) + 1;
    var endStr = docCookies.indexOf(";", cIndex);
    if(endStr == -1) {
        endStr = docCookies.length;
    }
    return unescape(docCookies.substring(cIndex, endStr));
}
jscript.storage.deleteCookie = function(inName) {
    if(this.getCookie(inName)) {
        this.setCookie(inName, null, "Thu, 01-Jan-1970 00:00:01 GMT");
    }
}
构建jscript.string包,如何计算一个子串在字符串中出现的次数?
jscript.string.substrCount = function(inStr, inSearchStr) {
    if(inStr == null || inStr == "" ||
        inSearchStr == null || inSearchStr == "") {
        return 0;
    }
    var splitChars = inStr.split(inSearchStr);
    return splitChars.length - 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值