function basis using

sort order, where ab is Liezi ascending, descending BA
the let ARR = [1,564,46,12,4, -455,];
the console.log (arr.sort (function (A, B) {
    return BA;
} ))

above, which is ascending, -1 and 1 descending replace what was
the let ARR = [1,654,13,41,3,4,5]
the console.log (arr.sort (function (A, B) {
    IF ( A <B) {
        return -1;
    } the else IF (A> B) {
        return. 1;
    } the else {
        return 0;
    }
}))

infex usage
let arr = [ "h", "e", "l", " L "," O "];
the console.log (arr.indexOf (" L ")) // here 2; subscript numbers at a positive position 2;
the console.log (arr.lastIndexOf (" L " )) // 3 here, but it is backwards looking forward to a positive number subscript, small labeled at 3;
console.log (arr.indexOf ( "z")) // not found, or -1 ;

the let ARR = [ ". 1,","2","3"]
console.log (arr.indexOf (1)); // here -1; using congruent Comparative

// includes for viewing array contains an element, comprising Returns true, otherwise returns to false
the let ARR = [ ". 1", "2", ". 3"]
the console.log (arr.includes (2)); // to false
the console.log (arr.includes ( "2")); // to true
the console.log (ARR. Includes (. 7)); // to false

// indexOf weight to
the let STR = [];
the let ARR = [1,2,6,1,2,3,1,5,2,3,1]
for (I = 0; I <arr.length; I ++) {
    IF (str.indexOf (ARR [I]) == -. 1) {
        str.push (ARR [I])
    }
} the console.log (STR)

Includes deduplication
let arr = [1,2,3,4,1,2,3,4,1,2,3]
the let STR = [];
for (I = 0; I <arr.length; I ++) {
    IF (str.includes (ARR [I]) == to false) {
        str.push (ARR [I])
    }
}
the console.log (STR);


Set
the let new new S1 = the Set ();
the let new new the Set S2 = ([l, 2,3])
the console.log (S1);
the console.log (S2)


the let new new S1 = the Set ();
s1.add (. 1) ;
the console.log (S1);
s1.add (2) .add (. 3) .add (. 4);
the console.log (S1)

// array may also be passed directly to
the let the Set new new S1 = ();
s1.add ([1,2,3,4]);
console.log (s1)

// the delete to delete an element, even if the array element will not be there without this error
// delete the array can not
let s1 = new Set ([1 , 2,3]);
s1.delete (2);
the console.log (S1);
s1.delete (. 4);
the console.log (S1);

// Clear delete all the elements
let s1 = new Set ([1 , 2,3,4])
s1.clear ();
the console.log (S1);

// for on-set of traverse can
let s = new set ([1,2,3,4,5,6 ] );
for (the let I of S) {
    the console.log (I);
}

// ... elements, the array can be set into
the let the Set new new S1 = ([l, 2,3]);
the console.log (S1);
the let ARR = [... S1];
the console.log (ARR);

// set switch array deduplication
the let ARR = [1,2, 3,1,2,3,1,5,2,3,41,3,1];.
the let the set new new = str (arr);
the let S = [str ...]
console.log (S)

// mapping, create an empty map objects, add a set of elements to the inside, from the collection to obtain information get
A new new = the Map the let ();
a. Set ( "name", "wangweijian");
a. Set ( "Age", 21 is);
the console.log (A) {// the Map 'name' => 'wangweijian' , 'Age' =>} 21 is
the console.log (a.get ( "name")) // wangweijian

// in a subject, the object can not be used as an object attribute keys, but the map in the map, you can do so , so to speak
// map map which can be used in any type of data as a key;
the let A = the map new new ();
a. Set ({}, "wangweijian");
a.set([1,2,3],21);
a. Set ([3554], 21 is);
the console.log (A); {} = {// the Map> 'wangweijian', [. 1, 2,. 3] => 21 is, [3554] => 21 is}

// Switch array mapping
// map structure into an array structure, or the use of relatively rapid method described earlier extended operator ....
ARR = the let [[ "name", "wangweijian"], [ "Age", 21 is]];
the let new new W = the Map (ARR);
the console.log ([... w.keys ()]); // [ 'name', 'age' ] show only key
console.log ([... w.values ()] ); // [ 'wangweijian', 21] displays only the value
console.log ([... w. entries ()]); // [ [ 'name', 'wangweijian'], [ 'age', 21]] display key and value
console.log ([... w]) // [[ 'name', 'wangweijian'], [ 'age ', 21]] values and display key


function
function Test (name) {
    the console.log ( "the Hello," + name);
}
Test ( "wangweijian"); // the Hello,




}
Test ( "wangweijian"); // the Hello, wangweijian

// main use is for protecting the arguments passed to the function the actual argument
function text (X) {
    for (the let I = 0; I <The arguments.length; I ++) {
        the console.log (arguments [I])
    }
}
text (l, 2,3);
//. 1
// 2
//. 3

// dummy array object that looks like an array of objects only, but not really array, we can prove it
function Test (X) {
    arguments.push (100); // array method for pseudo array object
}
Test (l, 2,3) // TypeError: Not iS a function arguments.push
it is not an array //

// variable parameter is to add three points in front of the last parameter, if not directly on the final error,
@ as: function test (... a, b ), will put all arguments into which an array, such as:
function Test (a, B ...) {
    the console.log (a); //. 1
    the console.log (B); // [2,3,4,5,6,7 , 8]
}
Test (1,2,3,4,5,6,7,8)

// this variable parameter is a real array, the array may be used in the method related
function text (A, B ...) {
    the console.log (A); //. 1
    the console.log (B) // [2,. 3,. 4,. 5,. 6]
    b.push (122);
    the console.log (B); // [2,. 3,. 4,. 5 , 6, 122] is used here to join the push element array 122
}
text (1,2,3,4,5,6)

// default parameters ES6 new
function text (name = "World") {
    Console. log ( "the Hello," + name);
}
text ( "wangweijian"); // the Hello, wangweijian
text () // the Hello, World

// if the parameter is an array, the array waist set the default value, the wording is slightly different: such as:
the let function Fn = ([A =. 1, B = 2] = []) {
    the console.log (A, B);
}
Fn () // 2. 1
Fn ([3,4-]) // 34

// comprises behind our object to be described, it is also possible provided movable default values, but similarly written above
let fn = function ({name = "wangweijian",age=18}={}){
    the console.log (name, Age);
}
Fn (); // wangweijian 18 is
Fn ({name: "Song", Age: 20 is}); Song 20 is //

// length attribute
let test = function (a, b , C) {
    the console.log (the arguments.length); // number of actual arguments 5
    console.log (arguments.callee.length); // number of parameters in the form of 3
}
Test (1,2,3,4, 5)

 

Caller property is not arguments object, but rather a function of object properties itself, it shows the caller of the function, if the function is (browser) is called global execution environment, then its value is null , if in another the function is called, it is the value of that function.

Global execution environment is called:

Browser:

<body>

   <script>        

let test = function(){            

console.log(test.caller);        }        

test();//null   

  </script>

</body>

Node in

let test = function(){    

console.log(test.caller);

}

 test();//[Function]

It is a function call:

let test = function(){    

let test2 = function(){       

  console.log(test2.caller);

        //[Function: test]      

  // ฎbecause the caller of the function is text function

}    

test2 ();

}

test();

 

Callee is arguments a property of an object, this property is a pointer to have the arguments object function

let test = function(){

let test2 = function(){

let test3 = function(){

console.log(arguments.callee);

//[Function: test3]

}

Test3 ();

}

Text2();

}

Test();

callee role is to be able to find arguments function object belongs, only the function name associated with the implementation and not to function together, we look at the following example

// calculate the factorial of a recursive function

let test = function(i){

if(i == 1){

return 1;

}else{

Return i * test (i-1 ); // on the function name and tightly linked up here

}

}

console.log(test(3));

If we take the above wording minor modifications, you can see the wording of the above defects

// calculate the factorial of a recursive function

let test = function(i){

if(i == 1){

return 1;

 }else{

return i * test (i-1 ); // on the function name and associate tightly together here

}

}

let test2 = test; // the factorial function assigned to text2

// change test function body of the factorial function

test = function(){

  console.log ( " I have changed "};

}

console.log(test2(3));

// I've changed

// NaN

 

So, this time you can use the arguments object callee property to reduce this association

// calculate the factorial of a recursive function

let test = function(i){

if(i == 1){

return 1;

}else{

I * The arguments.callee return (. 1-I) ; // the callee that owns the arguments object function

}

}

Test test2 = the let ; // J the factorial function assigned to test2

// change test function body of the factorial function

test = function(){

 console.log ( " I have changed ");

}

console.log(test2(3))//6

Guess you like

Origin www.cnblogs.com/wwjljwx/p/11119180.html