common use api
effective method
JS特性可选链式操作符 ?. 和 ?? 使用
const object = { id: 123, names: { first: 'Alice', last: 'Smith' }};
{
const firstName = object?.names?.first ?? '(no first name)';
const middleName = object?.names?.middle ?? '(no middle name)';
}
利用Object.prototype.toString准确判断类
type和instanceof都不能准确判断
var type = function(data) {
var toString = Object.prototype.toString;
var dataType =
data instanceof Element
? "element"
: toString
.call(data)
.replace(/\[object\s(.+)\]/, "$1")
.toLowerCase()
return dataType
};
数字位数处理
(Math.floor(x * 100) / 100).toFixed(2); // floor是向下取整,如需四舍五入请使用round
null -> 0.00
undefined -> 0.00
'123' -> 123.00
'1.' -> 1.00
'1.2' -> 1.20
'1.234' -> 1.23
'1.245' -> 1.24
'1.246' -> 1.26
RN for web
Node.js
WebAssembly