In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are the tool functions commonly used in JS development", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "what are the commonly used tool functions in JS development" this article.
1. IsStatic: check whether the data is the original data except symbol.
Function isStatic (value) {return (typeof value = 'string' | | typeof value = =' number' | | typeof value = = 'boolean' | | typeof value = =' undefined' | | value = null)}
2. IsPrimitive: check whether the data is the original data
Function isPrimitive (value) {return isStatic (value) | | typeof value = = 'symbol'}
3. IsObject: determine whether the data is of reference type (for example: array,function,object,regexe,new Number (), new String ())
Function isObject (value) {let type = typeof value;return value! = null & & (type = = 'object' | | type = =' function');}
4. IsObjectLike: check whether value is a class object. If a value is a class object, it should not be null, and the result after typeof is "object".
Function isObjectLike (value) {return value! = null & & typeof value = = 'object';}
5. GetRawType: get the data type, and the returned results are Number, String, Object, Array, etc.
Function getRawType (value) {return Object.prototype.toString.call (value) .slice (8,-1)} / / getoRawType ([]) ⇒ Array
6. IsPlainObject: determine whether the data is of Object type.
Function isPlainObject (obj) {return Object.prototype.toString.call (obj) = ='[object Object]'}
7. IsArray: determine whether the data is of array type (compatible with Array.isArray)
Function isArray (arr) {return Object.prototype.toString.call (arr) ='[object Array]'} / / Mount isArray to Array Array.isArray = Array.isArray | | isArray
8. IsRegExp: determine whether the data is a regular object
Function isRegExp (value) {return Object.prototype.toString.call (value) = ='[object RegExp]'}
9. IsDate: determine whether the data is a time object
Function isDate (value) {return Object.prototype.toString.call (value) = ='[object Date]'}
10. IsNative: to determine whether value is a browser built-in function, the body code block after the built-in function toString is [native code], while the non-built-in function is the related code, so the non-built-in function can be copied (after toString, the head and tail are pinched and then transferred by Function)
Function isNative (value) {return typeof value = = 'function' & & / native code/.test (value.toString ())}
11. IsFunction: check whether value is a function
Function isFunction (value) {return Object.prototype.toString.call (value) = ='[object Function]'}
12. IsLength: check whether value is a valid class array length
Function isLength (value) {return typeof value = = 'number' & & value >-1 & & value% 1 = = 0 & & value abCdEf// uses the memory function let _ camelize = cached (camelize)
17. Hyphenate: hump naming horizontal line naming: split strings, use-concatenation, and convert to lowercase
Let hyphenateRE = /\ B ([Amurz]) / ginto function hyphenate (str) {return str.replace (hyphenateRE,'- $1'). ToLowerCase ()} / / abCd = = > ab-cd// uses the memory function let _ hyphenate = cached (hyphenate)
18. Capitalize: the first bit of a string is capitalized
Function capitalize (str) {return str.charAt (0). ToUpperCase () + str.slice (1)} / / abc = = > Abc// uses the memory function let _ capitalize = cached (capitalize)
19. Extend: mix attributes into the target object
Function extend (to, _ form) {for (let key in _ form) {to [key] = _ form [key];} return to}
20. Object.assign: object attribute copy, shallow copy
Object.assign = Object.assign | | function () {if (arguments.length = = 0) throw new TypeError ('Cannot convert undefined or null to object'); let target = arguments [0], args = Array.prototype.slice.call (arguments, 1), key;args.forEach (function (item) {for (key in item) {item.hasOwnProperty (key) & & (target [key] = item [key])}) return target}
Use Object.assign to clone an object with money:
Let clone = Object.assign ({}, target)
Simple deep cloning can use JSON.parse () and JSON.stringify (), which parse json data, so only primitive types and arrays and objects other than symbol can be parsed.
Let clone = JSON.parse (JSON.stringify (target))
21. Clone: clone data, but deep cloning. Here is a list of original types, time, regularity, error, array, object cloning rules. Others can be supplemented by themselves.
Function clone (value, deep) {if (isPrimitive (value)) {return value} if (isArrayLike (value)) {/ / is a class array value = Array.prototype.slice.call (vall) return value.map (item = > deep? Clone (item, deep): item)} else if (isPlainObject (value)) {/ / is the object let target = {}, key;for (key in value) {value.hasOwnProperty (key) & & (target [key] = deep? Clone (value [key], value [key])}} let type = getRawType (value); switch (type) {case 'Date':case' RegExp':case 'Error': value = new window [type] (value); break;} return value}
22. Identify various browsers and platforms
/ / the running environment is browser let inBrowser = typeof window! = 'undefined';// running environment is Wechat let inWeex = typeof WXEnvironment! =' undefined' & &!! WXEnvironment.platform;let weexPlatform = inWeex & WXEnvironment.platform.toLowerCase (); / / browser UA judges let UA = inBrowser & & window.navigator.userAgent.toLowerCase (); let isIE = UA & & / msie | trident/.test (UA); let isIE9 = UA & UA.indexOf ('msie 9.0') > 0 Let isEdge = UA & & UA.indexOf ('edge/') > 0 android' isAndroid = (UA & & UA.indexOf (' android') > 0) | | (weexPlatform = 'android'); let isIOS = (UA & & / iphone | ipad | ipod | ios/.test (UA)) | (weexPlatform =' ios'); let isChrome = UA & & / chrome\ /\ d+/.test (UA) & &! isEdge
23. GetExplorerInfo: get browser information
Function getExplorerInfo () {let t = navigator.userAgent.toLowerCase (); return 0
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.