摘要:它會(huì)指出一個(gè)類是繼承自另一個(gè)類的。測(cè)試測(cè)試代碼來源頁(yè)面倒計(jì)時(shí)的一段運(yùn)用倒計(jì)時(shí)的一段腳本。截止日期符合日期格式,比如等有效日期。截止的天數(shù)小時(shí)分鐘秒數(shù)組成的對(duì)象。
清楚節(jié)點(diǎn)內(nèi)的空格
function cleanWhitespace(element) { //如果不提供參數(shù),則處理整個(gè)HTML文檔 element = element || document; //使用第一個(gè)節(jié)點(diǎn)作為開始指針 var cur = element.firstChild; //一直循環(huán),直到?jīng)]有子節(jié)點(diǎn)為止。 while (cur != null) { //如果節(jié)點(diǎn)是文本節(jié)點(diǎn),并且只包含空格 if ((cur.nodeType == 3) && !/S/.test(cur.nodeValue)) { element.removeChild(cur); } //一個(gè)節(jié)點(diǎn)元素 else if (cur.nodeType == 1) { //遞歸整個(gè)文檔 cleanWhitespace(cur); } cur = cur.nextSibling; //遍歷子節(jié)點(diǎn) } }
代碼來源:https://gist.github.com/hehongwei44/9105deee7b9bde88463b
JavaScript中的類繼承實(shí)現(xiàn)方式/** * 把一個(gè)實(shí)例方法添加到一個(gè)類中 * 這個(gè)將會(huì)添加一個(gè)公共方法到 Function.prototype中, * 這樣通過類擴(kuò)展所有的函數(shù)都可以用它了。它要一個(gè)名稱和一個(gè)函數(shù)作為參數(shù)。 * 它返回 this。當(dāng)我寫一個(gè)沒有返回值的方法時(shí),我通常都會(huì)讓它返回this。 * 這樣可以形成鏈?zhǔn)秸Z(yǔ)句。 * * */ Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; /** * 它會(huì)指出一個(gè)類是繼承自另一個(gè)類的。 * 它必須在兩個(gè)類都定義完了之后才能定義,但要在方法繼承之前調(diào)用。 * * */ 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方法對(duì)每個(gè)參數(shù)進(jìn)行循環(huán)。每個(gè)名稱, * 它都將parent的原型中的成員復(fù)制下來到新的類的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
將單個(gè)字符串的首字母大寫/** * * 將單個(gè)字符串的首字母大寫 * */ 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的類型檢測(cè)方式->typeof、constuctor。 * 推薦通過構(gòu)造函數(shù)來檢測(cè)變量的類型。 */ var obj = {key:"value"}, arr = ["hello","javascript"], fn = function(){}, str = "hello js", num = 55, bool = true, User = function(){}, user = new User(); /*typeof測(cè)試*/ 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測(cè)試*/ 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
頁(yè)面倒計(jì)時(shí)的一段運(yùn)用/** * * @descition: 倒計(jì)時(shí)的一段腳本。 * @param:deadline ->截止日期 符合日期格式,比如2012-2-1 2012/2/1等有效日期。 * @return -> 截止的天數(shù)、小時(shí)、分鐘、秒數(shù)組成的object對(duì)象。 */ function getCountDown(deadline) { var activeDateObj = {}, currentDate = new Date().getTime(), //獲取當(dāng)前的時(shí)間 finalDate = new Date(deadline).getTime(), //獲取截止日期 intervaltime = finalDate - currentDate; //有效期時(shí)間戳 /*截止日期到期的話,則不執(zhí)行下面的邏輯*/ if(intervaltime < 0) { return; } var totalSecond = ~~(intervaltime / 1000), //得到秒數(shù) toDay = ~~(totalSecond / 86400 ), //得到天數(shù) toHour = ~~((totalSecond - toDay * 86400) / 3600), //得到小時(shí) tominute = ~~((totalSecond - toDay * 86400 - toHour * 3600) / 60), //得到分?jǐn)?shù) 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
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/87691.html
摘要:把中的偽數(shù)組轉(zhuǎn)換為真數(shù)組在中,函數(shù)中的隱藏變量和用獲得的元素集合都不是真正的數(shù)組,不能使用等方法,在有這種需要的時(shí)候只能先轉(zhuǎn)換為真正的數(shù)組。檢測(cè)元素是否支持某個(gè)屬性代碼用法創(chuàng)建和使用命名空間使用方式 把JavaScript中的偽數(shù)組轉(zhuǎn)換為真數(shù)組 在 JavaScript 中, 函數(shù)中的隱藏變量 arguments 和用 getElementsByTagName 獲得的元素集合(Nod...
摘要:代碼來源一些常用的操作方法介紹查找相關(guān)元素的前一個(gè)兄弟元素的方法。查找元素指定層級(jí)的父元素。 DOM操作的增強(qiáng)版功能函數(shù) /** * 將一個(gè)DOM節(jié)點(diǎn)、HTML字符串混合型參數(shù) * 轉(zhuǎn)化為原生的DOM節(jié)點(diǎn)數(shù)組 * * */ function checkElem(a) { var r = []; if (a.constructor != Array) { ...
摘要:將加法和加上校驗(yàn)位能被整除。下面分別分析出生日期和校驗(yàn)位檢查生日日期是否正確輸入的身份證號(hào)里出生日期不對(duì)將位身份證轉(zhuǎn)成位校驗(yàn)位按照的規(guī)定生成,可以認(rèn)為是數(shù)字。校驗(yàn)位按照的規(guī)定生成,可以認(rèn)為是數(shù)字。表示全部為中文為不全是中文,或沒有中文。 判斷是否是合理的銀行卡卡號(hào) //Description: 銀行卡號(hào)Luhm校驗(yàn) //Luhm校驗(yàn)規(guī)則:16位銀行卡號(hào)(19位通用): // 1.將...
摘要:初始化參數(shù)可選參數(shù),必填參數(shù)可選,只有在請(qǐng)求時(shí)需要參數(shù)可選回調(diào)函數(shù)可選參數(shù)可選,默認(rèn)為參數(shù)可選,默認(rèn)為創(chuàng)建引擎對(duì)象打開發(fā)送普通文本接收文檔將字符串轉(zhuǎn)換為對(duì)象最后,說明一下此函數(shù)的用法。即等待與成功回調(diào),后標(biāo)志位置為。 jquery限制文本框只能輸入數(shù)字 jquery限制文本框只能輸入數(shù)字,兼容IE、chrome、FF(表現(xiàn)效果不一樣),示例代碼如下: $(input).keyup(...
通過數(shù)組,拓展字符串拼接容易導(dǎo)致性能的問題 function StringBuffer() { this.__strings__ = new Array(); } StringBuffer.prototype.append = function (str) { this.__strings__.push(str); return this; } StringBuffer....
閱讀 2674·2023-04-26 00:42
閱讀 2817·2021-09-24 10:34
閱讀 3835·2021-09-24 09:48
閱讀 4168·2021-09-03 10:28
閱讀 2589·2019-08-30 15:56
閱讀 2783·2019-08-30 15:55
閱讀 3276·2019-08-29 12:46
閱讀 2255·2019-08-28 17:52