国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript常用代碼段搜集

2024-05-06 16:11:24
字體:
供稿:網(wǎng)友
這篇文章主要記錄了本人搜集的幾段javascript常用代碼段,都是平時項目中需要用到的,需要的朋友可以參考下
 
 

1.json轉(zhuǎn)字符串

 

復(fù)制代碼代碼如下:

function json2str(o) {
    var arr = [];
    var fmt = function (s) {
        if (typeof s == 'object' && s != null) return json2str(s);
        return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
    };
    for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
    return '{' + arr.join(',') + '}';
}

 

2.時間戳轉(zhuǎn)為Date

 

復(fù)制代碼代碼如下:

function fromUnixTime(timeStamp) {
    if (!timeStamp || timeStamp < 1000 || timeStamp == ' ') return "";
    var theDate = new Date(parseInt(timeStamp) * 1000);
    return theDate;
}

 

3.Data-format

 

復(fù)制代碼代碼如下:

// 作者: meizz  
// 對Date的擴展,將 Date 轉(zhuǎn)化為指定格式的String   
// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個占位符,   
// 年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數(shù)字)   
// 例子:   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2012-12-02 08:12:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2012-12-02 8:12:4.18   
Date.prototype.Format = function(fmt) {  
    var o = {
        "M+": this.getMonth() + 1,                 //月份   
        "d+": this.getDate(),                    //日   
        "h+": this.getHours(),                   //小時   
        "m+": this.getMinutes(),                 //分   
        "s+": this.getSeconds(),                 //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

 

4.日期上增加n天

 

復(fù)制代碼代碼如下:

function addDay(number) {
        return fromUnixTime(new Date().getTime() / 1000 + 24 * 60 * 60 * number);
}

 

5. 使用 iframe 時,父窗體與子窗體之間的相互調(diào)用

 

復(fù)制代碼代碼如下:

// 父窗體調(diào)用子窗體內(nèi)的函數(shù)  
window.frames['ifm_id'].valueChange("id_101");  
// 子窗體調(diào)用父窗體的函數(shù)  
parent.refreshTree("nodeId_202");  

 

6. 彈出窗體與返回值

 

復(fù)制代碼代碼如下:

// 彈出窗體  
var url = "http://www.baidu.com";  
win=window.showModalDialog(url,window,"dialogLeft:400;dialogTop:200;dialogWidth:560px;dialogHeight:380px;scroll:yes;menubar:no;toolbar:no;status:no;");  
// 在彈出窗體中設(shè)置返回值  
var result = new Array();  
result[0] = "id_101";  
result[1] = "name_202";  
window.returnValue = result;  
window.close();  

 

7. javascript 作用域[只有全局作用域和函數(shù)作用域,javascript沒有塊作用域]

 

復(fù)制代碼代碼如下:

// 1. 全局作用域  
var id = "global variable";    // 1.1 在函數(shù)外部定義的變量  
function showMsg(){      
    message = "global message";// 1.2 未定義而直接賦值的變量  
                               //     在第一次使用時被定義為全局變量  
}  
// 2. 函數(shù)作用域  
function doCheck(){  
    var data = "function data";// 2.1 在函數(shù)內(nèi)部定義的變量  
}  

 

8. javascript 繼承機制

 

復(fù)制代碼代碼如下:

// 1. 對象冒充繼承  
function Person(strName){  
    // private fields  
    var name = strName;  
    // public methods  
    this.getName = function(){  
        return name;  
    };      
}  
function Student(strName,strSchool){  
    // 定義父類的屬性及方法      
    this.parent = Person;  
    this.parent(strName);  
    delete this.parent;        // 刪除臨時變量 parent  
    // 定義新屬性及方法      
    // private fields  
    var school = strSchool;  
    // public methods  
    this.getSchool = function(){  
        return school;  
    };       
}  
// 2. Funtion 對象的 call(..) 或 apply(..) 繼承  
//    call 和 apply 的區(qū)別在于:  
//      call  的第二個參數(shù)為可變參數(shù);  
//      apply 的第二個參數(shù)為 Array;  
function Animal(strName,intAge){  
    // private fields  
    var name = strName;  
    var age = intAge;  
    // public methods  
    this.getName = function(){  
        return name;  
    };   
    this.getAge = function(){  
        return age;  
    };  
}  
function Cat(strName,intAge,strColor){  
    // 定義父類的屬性及方法      
    Animal.call(this,strName,intAge);  
    // Animal.apply(this,new Array(strName,intAge));  
    // 定義新屬性及方法      
    // private fields  
    var color = strColor;  
    // public methods  
    this.getInfo = function(){  
        return "name:" + this.getName() + "/n"  
             + "age:" + this.getAge() + "/n"  
             + "color:" + color;  
    };  
}  
// 3. prototype 繼承  
//    prototype 聲明的屬性及方法被所有對象共享  
//    prototype 只有在讀屬性的時候會用到  
Function.prototype.extend = function(superClass){  
    // 此處的 F 是為了避免子類訪問父類中的屬性 this.xxx  
    function F(){};  
    F.prototype = superClass.prototype;  
    // 父類構(gòu)造函數(shù)  
    this.superConstructor = superClass;  
    this.superClass = superClass.prototype;  
    this.prototype = new F();  
    this.prototype.constructor = this;  
};  
Function.prototype.mixin = function(props){      
    for (var p in props){          
        this.prototype[p] = props[p];          
    }  
};  
function Box(){}  
Box.prototype = {      
    getText : function(){  
        return this.text;  
    },  
    setText : function(text){  
        this.text = text;  
    }  
};  
function CheckBox(){}  
CheckBox.extend(Box);  
CheckBox.mixin({  
    isChecked : function(){  
        return this.checked;  
    },  
    setChecked : function(checked){  
        this.checked = checked;  
    }  
});  

 

9. call , apply & bind

 

復(fù)制代碼代碼如下:

// thisArg 表示在 fun 內(nèi)部時 this 所指示的對象  
// call & apply 將立即執(zhí)行 fun 并返回結(jié)果  
var result = fun.call(thisArg,arg1,...);  
var result = fun.apply(thisArg,[argsArray]);  
// thisArg 表示在 fun 內(nèi)部時 this 所指示的對象  
// bind 返回的是一個匿名函數(shù)  
var tmpfun = fun.bind(thisArg);  
var result = tmpfun(arg1,...);  

 

 

復(fù)制代碼代碼如下:

<script type="text/javascript">  
/** 
 * 擴展 Function 的功能 
 */  
Function.prototype.bind = function(obj){  
    var method = this;  
    var tmpfun = function(){  
        return method.apply(obj,arguments);  
    };  
    return tmpfun;  
}  
function Parent(){  
    this.name = "parent";  
}  
function Child(){  
    this.name = "child";  
    this.getName = function(time){  
        return time + " " + this.name;  
    };  
}  
var parent = new Parent();  
var child = new Child();  
alert(child.getName(1));                // show 1 child  
alert(child.getName.call(parent,2));    // show 2 parent [call & apply 會立即執(zhí)行]  
var tmpfun = child.getName.bind(parent);// bind 不會立即執(zhí)行  
alert(tmpfun(3));                       // show 3 parent  
</script>  

 

10. js "==" Operator

 

復(fù)制代碼代碼如下:

轉(zhuǎn)換規(guī)則  
   如果一個操作數(shù)是 Boolean 值,則比較之前將其轉(zhuǎn)成數(shù)字:false -> 0, true -> 1;  
   如果一個操作數(shù)是數(shù)字,另一操作數(shù)是字符串,則比較之前將字符串轉(zhuǎn)成數(shù)字;  
   如果一個操作數(shù)是對象,另一操作數(shù)是數(shù)字或字符串,則比較之前會將對象轉(zhuǎn)為基本類型,  
       引擎會先嘗試調(diào)用 valueOf(),如果 valueOf() 沒有 override 或返回一個對象,  
       則引擎會嘗試調(diào)用 toString(),如果 toString() 沒有 override 或返回一個對象,則拋出異常;  
   如果是兩個對象進行比較,則判斷它們是否引用同一對象;  
   如果一個操作數(shù)是 NaN, == 將返回 false, != 將返回 true;  
   null 和 undefined 與其它值比較將返回 false,  
       但 null == null, undefined == undefined, null == undefined;  
   參與比較時 null 和 undefined 不能轉(zhuǎn)為其它值;    

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 榕江县| 安阳县| 贡嘎县| 崇阳县| 昌黎县| 通海县| 阳山县| 海城市| 民勤县| 和硕县| 昭平县| 平罗县| 台北市| 泊头市| 阳城县| 达拉特旗| 会宁县| 抚州市| 绥宁县| 上栗县| 萝北县| 灵武市| 广德县| 民勤县| 庆元县| 永兴县| 桂林市| 托里县| 文山县| 麦盖提县| 河北省| 金坛市| 静安区| 伊宁县| 昆山市| 弥勒县| 台中县| 辽阳市| 抚远县| 呼伦贝尔市| 临邑县|