JSON.stringify
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
相关问题
如何判断一个函数是否为JavaScript原生函数
Array.prototype.push.toString() //function push() { [native code] }
function func(){}
func.toString() // function func(){}
通过调用函数的toString方法,判断返回的字符串中是否包含 [native code]。
// 借助于lodash
_.isNative(Array.prototype.push);
// => true
_.isNative(_);
// => false
//lodash 的实现
function isObject(value) {
const type = typeof value
return value != null && (type === 'object' || type === 'function')
}
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
const reIsNative = RegExp(`^${
Function.prototype.toString.call(Object.prototype.hasOwnProperty)
.replace(reRegExpChar, '\\$&')
.replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?')
}$`);
// reIsNative => /^function.*?\(\) \{ \[native code\] \}$/
function isNative(value) {
return isObject(value) && reIsNative.test(value)
}
export default isNative