JS basis - Global built-in objects

Objects

JS There are those built-in objects

  • Data encapsulation class object
    • String、Array、Object、Boolean、Number
  • Other objects
    • Math、Date、RegExp、Error、Function、Arguments
  • ES6 new objects
    • Promise、Map、Set、Symbol、Proxy、Reflect

Array Array object common methods

Method modifier
following these methods will change the value of the object to call their own :

  • Array.prototype.pop()
    • Removes the last element of the array and returns that element.
  • Array.prototype.push()
    • Adding one or more elements to the end of the array, and returns the new length of the array.
  • Array.prototype.shift()
    • Remove the first element of the array and returns that element.
  • Array.prototype.unshift()
    • Adding one or more elements at the beginning of the array, and returns the new length of the array.
  • Array.prototype.splice()
    • Add or delete any elements to the array in any position.
  • Array.prototype.reverse()
    • Reversing the order of elements in the array, i.e. the original becomes a first last, becomes the last original first.
  • Array.prototype.sort()
    • Sort of array elements and returns the current array.
  • Array.prototype.fill()
    • The values ​​of all elements in the array of the designated section, are replaced with a fixed value.
  • Array.prototype.copyWithin()
    • Within the array, a segment of the sequence of elements copied onto another section of the sequence of elements, the old value.

Access method
following these methods will not change the value of their calling object will only return a new array or return an other expectations.

  • Array.prototype.join()
    • Connecting a string of all array elements.
  • Array.prototype.slice()
    • Extracting some elements of the current in the array are combined into a new array.
  • Array.prototype.concat()
    • It returns a new array and the current array of other combinations of a plurality of arrays or a plurality of values ​​obtained by non-array.
  • Array.prototype.includes()
    • Determine whether the current array contains the values ​​for a specified if it returns true, otherwise false.
  • Array.prototype.indexOf()
    • Returns the index of the first value equal to the specified element of the array, if no such element, or -1.
  • Array.prototype.lastIndexOf()
    • Returns an array of the last (first from right number) of the index value equal to a specified element, If no such element, or -1.
  • Array.prototype.toSource()
    • It returns an array of the current literal string representation. Obscured Object.prototype.toSource the prototype chain () method.
  • Array.prototype.toString()
    • Returns a string combination of all array elements formed. Obscured Object.prototype.toString the prototype chain () method.
  • Array.prototype.toLocaleString()
    • Returns a string of all array elements by the combination of the localized formed. Obscured Object.prototype.toLocaleString the prototype chain () method.

Iterative method

In the following a number of traversal methods, there are many ways to specify a callback function as a parameter. Before each array element separately after executing a callback function, length property of the array will be cached somewhere, so if you array is currently in the callback function to add new elements, so those elements are not newly added It will be traversed to. In addition, if the current array of other modifications in the callback function, such as changes in the value of an element or delete an element, then the subsequent traversal operation may be affected by unexpected. In short, do not try to make any changes to the original array traversal, although the specifications for such operations in detail the definition, but for readability and maintainability, please do not do this.

  • Array.prototype.forEach()
    • Each element in the array performs a callback function.
  • Array.prototype.map()
    • It returns a new array of the return value of the callback function of composition.
  • Array.prototype.reduce()
    • From left to right for each array element performs a callback function, and the return value of the last callback in a scratchpad passed to the next callback function, and returns the last return value of a callback function.
  • Array.prototype.filter()
    • Returns true in all the array elements into filter function and returns a new array.
  • Array.prototype.every()
    • If each element in the array satisfies the test function returns true, otherwise false.
  • Array.prototype.some()
    • If there is at least one element in the array to meet the test function returns true, otherwise false.
  • Array.prototype.find()
    • Meet the test function finds the first element and returns the value of that element, if not found, undefined is returned.
  • Array.prototype.findIndex()
    • Meet the test function finds the first element and returns the index of that element, if not found, -1 is returned.
  • Array.prototype.keys()
    • It returns an array iterator object, which iterator will contain all the key elements in an array.
  • Array.prototype.entries()
    • Returns an array iterator object, the iterator will contain all the key elements of an array of pairs.

String common API

  • String.prototype.split()
    • By separating the string to a string, the string object into an array of strings.
  • String.prototype.slice(start, end)
    • A removal string region, returns a new string.
  • String.prototype.substr(start, len)
    • It returns the character string specified by the start position of the specified number of characters.
  • String.prototype.substring()
    • Returns between two specified character string in the index.
  • String.prototype.trim()
    • Removing spaces from the beginning and end of a string
  • String.prototype.concat()
    • Connecting the two text strings and returns a new string.
  • String.prototype.match()
    • A regular expression character string is compared.
  • String.prototype.replace()
    • Is used in the regular expression and a direct comparison string, then the string is matched substring is replaced with a new child.
  • String.prototype.search()
    • Regular expression search and match the specified string and returns the first occurrence of the subscript match.
  • String.prototype.toString()
    • Returns a string represented by the particular object. Rewrite Object.prototype.toString method.

Set, the difference between Map, WeakSet and WeakMap of?

Set

  • There is no representation, value of the member is unique, no duplicate values
  • Accept an array (or iterative data structure) as a parameter
  • Note: The two objects are always unequal

Attributes:

  • Set.prototype.constructor: constructor, the default is Set function.
  • Set.prototype.size: Returns the total membership of the Set instance.

method:

  • add (value): add some value, return Set structure itself.
    • s.add(1).add(2).add(2);
  • delete (value): delete a value, it returns a Boolean value that indicates whether the deletion was successful.
  • has (value): returns a Boolean value that indicates whether the value is a member of Set.
  • clear (): remove all members and no return value.

Traversal methods

  • keys (): returns the key of the walker
  • values ​​(): returns the key iterator
  • entries (): returns the key-value pairs iterator
  • forEach (): Use callback function traverse each member

WeakSet

Set WeakSet similar structure, it is not a duplicate set of values. But there are several differences with the Set:

  • The members WeakSet only objects , but can not be other types of values
  • WeakSet the objects are weak references
    • If other objects are no longer referenced object, garbage collection automatically reclaims the memory occupied by the object
    • Dependent on reference counting garbage collection, if the number of times a reference value is not zero, the garbage collection mechanism can not release this memory. After the end of the value is used sometimes forget dereference, causing memory can not be released, which may cause a memory leak. WeakSet inside references, it is not included in the garbage collection mechanism, so the problem does not exist. Therefore, WeakSet suitable for temporary storage of a set of objects, as well as information stored with the object binding. As long as these objects disappear on the outside, inside it is referenced in WeakSet it will automatically disappear.
  • WeakSet not traversable
    • Since the internal WeakSet the number of members, depending on the garbage collection mechanism has not run before and after the run is likely the number of members is not the same, and when to run the garbage collection mechanism is unpredictable
  • WeakSet structure no clear method.

Map

Like object, but also a set of key-value pairs, but the scope of the "key" is not limited to character string, various kinds of values (including objects) Map can be used as keys .

The method of traversing
Map construct to provide three primary functions and traversal generates a traversal methods.

  • keys (): returns the key of the walker.
  • values ​​(): returns the key iterator.
  • entries (): Returns all members of the walker.
  • () ForEach: Map of traversing all members.

WeakMap

WeakMap design aims: Sometimes we want to store some data in an object above, but it will form a reference to the object, and once the two objects are no longer needed, we must delete this reference manual, or garbage collection They will not release the memory occupied by the referenced object.

Basically, if you want to add to the data on the object, do not want to interfere with garbage collection, you can use WeakMap.

A typical application scenario , the addition of the data element in the DOM of the page, can be used WeakMap structure. When the DOM element is cleared, its corresponding WeakMap record will be removed.

Guess you like

Origin www.cnblogs.com/nayek/p/11729872.html