Home  >  Article  >  Web Front-end  >  javascript_core_10

javascript_core_10

高洛峰
高洛峰Original
2016-10-12 13:14:251352browse

1. Inheritance between two existing objects: Object.setPrototypeOf (child, father);

javascript_core_10

2. Create a child object based on the existing parent object: var child=Object.create (father, {new attribute}) ;

3. Batch modify the parent objects of multiple sub-objects: before creating the first sub-object, modify the prototype of the constructor to a new object;

4. Inheritance between two types: multiple sub-types contain the same Properties and methods; ① Abstract parent type: define the same properties (methods) in the parent type constructor (prototype object); ② Borrow the parent type in the sub-type constructor: parent type constructor.apply (this, arguments) ;③ Set the subtype prototype object to inherit the parent type prototype object: Object.setPrototypeOf (subtype prototype object, parent type prototype object);

javascript_core_10

5. Judgment of the array API:

① Find the position of the specified element: indexOf/ lastIndexOf;

②Array.isArray(obj);

③Judge whether each element in the array meets the requirements: arr.every(function(val,idx,arr){return judgment condition;});④Judge whether it meets the requirements Element: arr.some(function(val,idx,arr){return judgment condition;});*(*where val: automatically obtain the current element value; idx: automatically obtain the current element position; arr: automatically obtain the current array being traversed ;return: judgment result;)

javascript_core_10

6. Traversal of array API:

① Perform the same operation on each element in the array: arr.forEach (function (val, idx, arr) { // for arr [ Modify the value of idx】);

② Based on the original array, process each element to generate a new array: arr.map (function (val, idx, arr) {//Return to the new array after modification according to val; return new value; });

javascript_core_10

7. Filter summary of array traversal:

①Copy the elements that meet the requirements in the original array to form a new array: var subArr=arr.filter (function (val, idx, arr) {return condition;});

javascript_core_10

② Summarize each element value in the array to produce a result: var r=arr.reduce(function (prev, val, idx, arr) {return the summary value of prev and val ;}, starting value); * ( * where prev is the current summary value;);

javascript_core_10

8. bind: Create a new function based on the existing function, and permanently bind this in the function to the specified object in advance;

call and apply: forcibly borrow a function, temporarily replace this in the function with the specified object (execute a function);

bind: create a new function, permanently bind this and some parameters (create a new function);

javascript_core_10

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn