判斷JS日期物件

// Method 1
typeof date.getMonth === 'function'
// Method 2
date instanceof Date
// Method 3
Object.prototype.toString.call(date) === '[object Date]'
view raw typeof.js hosted with ❤ by GitHub

Javascript Unicode

console.log('好'.charCodeAt()); //22909
console.log('好'.charCodeAt().toString(16)); //597d
console.log(String.fromCharCode(22909)); //好
console.log(String.fromCharCode(0x597d)); //好
console.log('\u597d'); //好
console.log('\u{597d}'); //好
//prototype 擴充類別定義
//方法1 (Regex)
String.prototype.toUnicode = function() {
return this.replace(/./g, function(char) {
var v = char.charCodeAt(0).toString(16);
var pad = "0000";
return (pad.substring(0, pad.length - v.length) + v).toUpperCase();
});
};
console.log('狂'.toUnicode()); //72C2
//方法2 (while)
String.prototype.toUnicode = function () {
return this.replace(/./g, function (char) {
var v = String.charCodeAt(char).toString(16);
if (v.length != 4) {
do {
v = '0' + v;
} while (v.length < 4)
}
return v;
});
};
console.log('狂'.toUnicode()); //72C2
view raw gistfile1.js hosted with ❤ by GitHub