JavaScript Tip summary: Standard library

Source: Internet
Author: User
Tags object object shallow copy

1. Object when used as a constructor, you can accept a parameter. If the argument is an object, the object is returned directly, and if it is a value of the original type, the wrapper object for that value is returned.

2. All constructors have a prototype property that points to a prototype object. All properties and methods defined on the Object.prototype object will be shared by all instance objects.

3. Object.keys methods and Object.getOwnPropertyNames methods are similar, and are typically used to traverse the properties of an object. Their arguments are all an object, returning an array whose members are all property names, not inherited, of the object itself. The difference is that Object.keys methods return only enumerable properties, and Object.getOwnPropertyNames methods also return non-enumerable property names.

4. Related methods of the object property model:

    • Object.getownpropertydescriptor (): Gets the attributes object for a property.
    • Object.defineproperty (): Defines a property through the Attributes object.
    • Object.defineproperties (): Defines multiple properties through the attributes object.
    • Object.getownpropertynames (): Returns the names of all properties directly defined above an object.

5. How to control the state of an object:

    • Object.preventextensions (): Prevents object expansion.
    • Object.isextensible (): Determines whether the object is extensible.
    • Object.seal (): Disables object configuration.
    • Object.issealed (): Determines whether an object can be configured.
    • Object.freeze (): Freezes an object.
    • Object.isfrozen (): Determines whether an object is frozen.

6. Prototype chain related methods:

    • Object.create (): Generates a new object and prototypes the object.
    • Object.getprototypeof (): Gets the object's prototype object.

7. Object the method of the instance object, mainly has the following six:

    • valueOf(): Returns the value corresponding to the current object.
    • toString(): Returns the string form corresponding to the current object.
    • toLocaleString(): Returns the local string form corresponding to the current object.
    • hasOwnProperty(): Determines whether a property is a property of the current object itself, or a property inherited from a prototype object.
    • isPrototypeOf(): Determines whether the current object is a prototype of another object.
    • propertyIsEnumerable(): Determines whether a property can be enumerated.

8. The method return values for the different data types Object.prototype.toString are as follows:

Object.prototype.toString.call(value) =>
    • Value: Returns [object number] .
    • string: Returns [object string] .
    • Boolean value: Returns [object Boolean] .
    • undefined: Returns [object undefined] .
    • null: Returns [object null] .
    • array: Returns [object array] .
    • Arguments object: Returns [object Arguments] .
    • function: Returns [object function] .
    • Error object: Returns [object Error] .
    • Date object: Returns [object Date] .
    • RegExp object: Returns [object RegExp] .
    • Other object: Returns [object Object] .

9. Array It is a built-in object of JavaScript and is also a constructor that you can use to generate new arrays.

The Array.isArray method is used to determine whether a value is an array. It can compensate typeof for the shortage of operators.

Methods for 11.Array instances:

  • The ValueOf () method returns the array itself.
  • The ToString () method returns the array string form
  • The
  • push () method is used to add one or more elements at the end of the array and returns the length of the array after the new element is added, noting that the method changes the array length. Push () can also add properties to an object, and the object has a length property (
      var a = {a: 1};[]. push. Call (a2 A //{a:1, 0:2, Length:1}[]. Push. Call (a[3a //{a:1, 0:2, 1:[3], length:2}                  
    )
  • The Pop () method deletes the last element of the array and returns the element. The method changes the original array.
  • The join () method returns all of the array members as a string, using the argument as a delimiter. No arguments are separated by commas.
  • The Concat () method is used for multiple array merges, adds a new array to the end of the original array, returns a new array, and does not change the original array. (If you do not supply a parameter, the concat method returns a shallow copy of the current array.) The so-called "shallow copy" means that if an array member includes a value of a compound type (such as an object), the new array is copied with a reference to that value. )
  • The shift () method is used to delete the first element of the array. and returns the element.
  • The Unshift () method is used to add elements to the first position of the array. Returns the length of the new array.
  • The reserve () method reverses the order of the elements in the array, returning the changed array.
  • The slice () method is used to extract part of the original array, returning the new array, and the original array unchanged. The first parameter is the start position, the second argument is the terminating position (the element at that position is not included), and the second argument is omitted to return the last member of the original array. The parameter is negative, which indicates the position of the reciprocal calculation.
  • The splice () method deletes a member of the original array and can add a new member at the deleted location, returning the value to the deleted element, and the method changes the original array. The first parameter is the delete start position, and the second parameter is the number of deletions. The new member is added if there are parameters later. If the starting position is negative, it is deleted from the countdown position. If you simply insert an element, splice the second parameter of the method can be set to 0. If only the first argument is supplied, it is equivalent to splitting the original array into two arrays at the specified location.
  • sort()method to sort the array members by default, sorted in dictionary order. After sorting, the original array will be changed. If you want sort the method to be ordered in a custom manner, you can pass in a function as an argument, which means sorting by custom method. The function itself accepts two parameters, representing two elements for comparison. If the return value 0 is greater than, indicates that the first element is followed by the second element, and in other cases, the first element is preceded by the second element.

JavaScript Tip summary: Standard library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.