Building wheels, some common methods of js

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 typeisObj() isArray() isUndefined() isNaN()
  • Singleton pattern wrappergetSingle()
  • 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 jQuery

    The 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 tools

    Similar to jQuery's extend method, the difference is: if the target object has the same properties,

    • If the property is not objector array, then do not copy, that is, do not overwrite the property in the target object
    • If the attribute is objector array, and the attribute type is the same, the attribute will be extended, otherwise it will not be processed
  • deepClone()
    var obj = _.deepClone({} | [] | function)Deep copy of object Input an object to make a deep copy, return an object with properties

  • Flattening of arraysflattenDepth()
  • Array deduplicationunique()
  • get variable typegetType()
  • The first letter of the string is uppercase, other characters are lowercasetoUp()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936650&siteId=291194637