Javascript Basics Common Objects Introduction

Source: Internet
Author: User

Array type (stacks && queues)

1) var arr = new Array (); The length in parentheses can be unspecified, because the length of the array in JS can be changed dynamically

2) Similar to the list container in Java, the length is variable (arr.length=num), the element type is arbitrary.

3) Arr.push (obj, obj, ...); Push allows you to add as many elements to an array as you like in the stack.

4) Arr.pop (); Pop allows you to delete the topmost element in the array. Delete elements in the same stack

5) arr.unshift (obj, obj,..); by unshift you can add any number of elements to an array, like a queue to add elements

6) Arr.shift (); You can delete the lowest-end element in the array by using shift. Delete elements in a similar queue

7) Arr.splice (NUM1, num2, num3,.. NUM1 represents the starting position of the array, num2 represents the number of captures (removed from the array), num3 and all subsequent elements represent the new element to be added to the array, and finally the array removes both the element and the new element.

8) arr = Arr.slice (NUM1, num2); left closed right, which means getting from the array from the second to the fourth element returns

9) Splice manipulate the array itself, slice does not manipulate the array itself, so slice does not change the original array object.

arr = Arr.concat (ARR2); Array elements are merged, the array itself is not manipulated, and its return element is the result of the operation

arr = Arr.join ('-'); add parameter contents between each element without manipulating the array itself

Arr.sort (); Sorts the array elements, the default is AES, and the internal comparison rule is to convert the elements to strings in the comparison. The custom comparison rule is a custom function and passes the function's object as a parameter into the sort (fun), the following function fun (obj1, obj2) {return 1; return 0; Return-1}.

Arr.reverse (); transpose the array element.

var value = arr.indexof (index); var value = Arr.indexof (start, index) similar to Arr[index]

Arr.lastindexof (index); Same as above

(Arr.reduce) (function (Pre, cur, "index, arr") {}); Array overlay, this method is still very simple to use.

Arr.reduceright (function (PRE, cur, "index, arr") {}); Arrays are stacked backwards, so there's no point in having the method above.

18) Method Funname (function (iteam, index) {}) of 5 kinds of iterated algebraic groups;

Every: A function is run for each element of the array, False if the function runs one time, or true

Some: Runs a function for each element of the array, true if the function runs one time, or False if it is true

Filter
: For each element of the array to run a function, the function performs a filter to return the filtered results. return true;
ForEach: Iterates through each value in the array and processes the value (function). is to iterate over the object, and he does not return the resulting value unless the value is changed using a reference object.
map: Iterates through each value in the array, and processes the value (function), and returns the processed result, the difference between the filter and filter is that it returns only the values that satisfy the filter, and the map returns the result after each value, and the array length is unchanged. And the filter is going to change. Return Iteam.

19) The array to the object, the object to the array, write their own conversion method, is actually using the JS feature, the array of duplicate values removed.


Object Type (Java-like object)

1) base class for all classes, var obj = {}; = new Object (); (preferably do not omit parentheses, format requirements)

2) Custom properties and methods, direct obj.name = "";  obj[' sex '] = ""; obj.fun= function () {};

3) Delete the properties and methods, delete obj.name; Delete Obj.fun;delete Obj[name]; Note The Delete method does not require parentheses

4) Traversal JS object, for in syntax; for (var attribute in object) {Object[attribute] + attribute}

5) The object object must have the method:

1 obj.  constructor; constructor, not parentheses, because parentheses represent the return value of a function, rather than calling the function itself object

2 obj. hasOwnProperty (' PropertyName '); Whether the object has properties

3 obj. isPrototypeOf (OBJ2); Testing prototypes

4 obj. propertyIsEnumerable (' PropertyName '); Detects if a property can be enumerated

5 obj. toLocaleString () A string representation of the local language

6 obj. ToString (); object to String

7 obj. ValueOf (); The string represents the object, and the Tolocalstring () method often returns the same value as the ToString () and ValueOf () methods

6) Override the method in object as long as Obj.funname = function () {}; is to rewrite.


Other reference type objects:

Monomer object: You can use objects without instantiating them; Global Math

Global Object

1) Global: Intangible, Global, Ultimate, top, non-existent objects. Because it does not exist, it cannot be instantiated, that is, it cannot be used with new, and it can be called directly by his method. The Global object is never used directly and cannot be created with the new operator. It is created when the Scripting engine is initialized and immediately makes its methods and properties available.

2) encodeURI (str); encodeuricomponent (str); and corresponding method decodeURI (str); decodeuricomponent (str); Notice the general use of both, The reason is that the former only handles whitespace in the URL, while the latter handles many characters

3) eval (str), the most powerful method, the invisible JavaScript parser; convert some strings to scripts or objects in JavaScript. This method is generally not used, because security, because any string code will be executed, unless you know that the code is not a problem to execute, use this method to note three points:

A:eval (str), where Str must be a string type, not an object type, meaning that the parameter of the descendant can only be a native string type, otherwise eval returns the object to the caller without any processing.

B:eval (' (' +objstr+ ') '); The purpose of this method is to convert a string to an object type, which is fixed, and the use of parentheses is the reason for the code block.  Objstr= "{name: ' DFF ', Sex: ' Man '}"; JSON object

C:eval (' 5+10 '); This form is to use JavaScript parsing to execute the JS code in STR, and then return the result, if no return value is undefiend.

4) parseint (str); parsefloat (str); type conversion

5) Escape (str); unescape (str); Encryption, decryption, this is similar to the above encodeURI, so there is no value.

6) IsNaN (obj); returns false if obj can be converted to Numner type, otherwise true;

7) isfinite (obj); Determines whether the object is infinite.

Math Object

1) math.funname (); Basic Arithmetic of mathematics

2) math.random (); The generation of random numbers. Returns a pseudo-random number between 0.0 and 1.0.

Date Object

1) var date = new Date ();

2) var year = Date.getfullyear (); Do not use date.getyear (); The reason is that the browser does not deal with this method

3) How to convert the date in JavaScript to the format you want, that is, to get the date and time by getxxx, and then stitch the string

4) GetTime (); Gets the function execution time by this difference.

Basic Package Type

1) Boolean String number


Javascript Basics Common Objects Introduction

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.