1.json轉字串
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.時間戳轉為Date
function fromUnixTime(timeStamp) {
if (!timeStamp || timeStamp var theDate = new Date(parseInt(timeStamp) * 1000);
return theDate;
}
3.Data-format
// 作者: meizz
// 對Date的擴展,將 Date 轉換為指定格式的String
// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可用 1-2 個佔位符,
// 年(y)可以用 1-4 個佔位符,毫秒(S)只能用 1 個佔位符(是 1-3 位的數字)
// 例:
// (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天
function addDay(number) {
return fromUnixTime(new Date().getTime() / 1000 24 * 60 * 60 * number);
}
5. 使用 iframe 時,父窗體與子窗體之間的相互呼叫
// 父窗體呼叫子窗體內的函數
window.frames['ifm_id'].valueChange("id_101");
// 子窗體呼叫父窗體的函數
parent.refreshTree("nodeId_202");
6. 彈出視窗與回傳值
// 彈出視窗
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;"); // 在彈出視窗中設定回傳值
var result = new Array();
result[0] = "id_101";
result[1] = "name_202";
window.returnValue = result;
window.close();
var id = "global variable"; // 1.1 在函數外部定義的變數
function showMsg(){
message = "global message";// 1.2 未定義而直接賦值的變數
// 造成初次使用時被定義為全域變數 }
// 2. 函數作用域
function doCheck(){
var data = "function data";// 2.1 在函數內部定義的變數
}
// 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 的差別在於:
// call 的第二個參數為變動參數;
// apply 的第二個參數為 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;
// 父類別建構子
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
// thisArg 表示在 fun 內部時 this 所指示的對象
// call & apply 將立即執行 fun 並傳回結果
var result = fun.call(thisArg,arg1,...);
var result = fun.apply(thisArg,[argsArray]);
// thisArg 表示在 fun 內部時 this 所指示的對象
// bind 回傳的是一個匿名函數
var tmpfun = fun.bind(thisArg);
var result = tmpfun(arg1,...);
10. js "==" Operator
轉換規則
如果一個運算元是 Boolean 值,則比較之前將其轉成數字:false -> 0, true -> 1;
如果一個運算元是數字,另一個運算元是字串,則比較之前將字串轉成數字;
如果一個運算元是對象,另一個運算元是數字或字串,則比較之前會將物件轉為基本型,
引擎會先嘗試呼叫 valueOf(),如果 valueOf() 沒有 override 或回傳一個對象,
則引擎會嘗試呼叫 toString(),如果 toString() 沒有 override 或回傳一個物件,則拋出例外;
如果是兩個物件進行比較,則判斷它們是否引用同一物件;
如果一個運算元是 NaN, == 會回傳 false, != 會回傳 true;
null 和 undefined 與其它值比較將回傳 false,
但 null == null, undefined == undefined, null == undefined;
參與比較時 null 和 undefined 不能轉為其它值;

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具