jsTools
Build wheels and implement some common js methods by yourself. Supports direct introduction in browsers and node environments, and also supports introduction using browser-side requirejs
=======
currently implemented method
- Determine the variable type
isObj() isArray() isUndefined() isNaN()
- Singleton pattern wrapper
getSingle()
-
Publish and subscribe module The
_.on() _.off() _.trigger()
above three functions are used to subscribe, remove, and trigger events respectively, and the usage is the same as jQueryThe difference is that I have added the concept of namespace, which can be used
_.create()
to create a namespace, which is isolated from the global and other namespaces, and can publish and subscribe to events of the same name without affecting each other, and there are two other methods,_.use() _.drop()
. The usage is as follows:var name2 = _.create('name1'); name2.on('event1',callback) _.create('name2').on('event2',callback); _.use('name1').trigger('event1'); _.drop('name1', 'name2', ...);//其返回一个数组,分别表示个命名空间删除是否成功
-
Inheritance and extension methods
_.extend() 和 _.fn.extend()
_.extend(true, target, {}, {})
The first parameter is the boolean value true, and a deep copy
_.fn.extend(true, {})
is directly extended to the prototype fn of toolsSimilar to jQuery's extend method, the difference is: if the target object has the same properties,
- If the property is not
object
orarray
, then do not copy, that is, do not overwrite the property in the target object - If the attribute is
object
orarray
, and the attribute type is the same, the attribute will be extended, otherwise it will not be processed
- If the property is not
-
deepClone()
var obj = _.deepClone({} | [] | function)
Deep copy of object Input an object to make a deep copy, return an object with properties - Flattening of arrays
flattenDepth()
- Array deduplication
unique()
- get variable type
getType()
- The first letter of the string is uppercase, other characters are lowercase
toUp()