web123456

Exercise (niukewang)

1. One of the following window methods can display a dialog box (C)

A: confirm()

B:alert()

C:prompt()

d:open()

Parsing:

confirm() is to pop up the judgment dialog;

alert() is a popup window;

prompt() displays a dialog box;

open() is to open a new page;

2. Perform the following procedures, the following statements are correct (A)

var arr = new Array(3); ...①

arr[0] = 1;

= 0;

console.log(); ...②

(value=>{

(value); ...③

})

for(var i in arr){

(arr[i]); ...④

}

A:①Equation create an array of length 3

B:② equation output result is 4

C: ③ type output result is 1 0

D:equation ④ outputs a result of 1

Parsing:

var arr = new Array(3); ... ① // The result is [empty, empty, empty].

arr[0] = 1; // the result is [1, empty , empty ])

= 0; // the result is [1, empty , empty , b:0]

                                                //By means of the dot operator (.) The addedattribute is at the same level as the length attribute and does not affect the value of length

(); ... ② // The result is 3

        (value=>{

(value); ... ③ // the result is 1 , because at this point the traversal is [1, empty , empty]

        })                                              //Through the dot operator (.) The added attributes can be used with the for... .in... loop traversal, but not foreach loop traversal.

         for(var i in arr){

(arr[i]); ... ④ // The result is 0 , because at this point the traversal is [b:0]

        }                                             //Through the dot operator (.) The added attributes can be used with the for... .in... loop traversal, but not foreach loop traversal.

Attention:

        Through the dot operator (.) Added can be traversed with a forin loop, but not with a foreach loop. And the added attribute is at the same level as the length attribute, so it won't affect the length value.

3. Execute the following program and the output will be (C)

function a(){

  (this);

(null);

A:document

B:null

C:window

D:a

E:undefined

Parsing:

call inheritance, object impersonation, because passing in null or undefind is the same as not passing in, so it's still a global window.

4. Execute the following program and the output will be (D)

let num = (function(x){delete x;return x;})(1);

(num);

A:Throw an exception

B:null

C:undefined

D:1

Parsing:

delete can only delete attributes of an object. If it is , it can be deleted using delete.

        Variables and variables in the prototype chain cannot be deleted.

5.Of the following methods provided by Promise, the one used to add a callback function to the success or failure callback function queue is ( D)

A:done

B:fail

C:always

D:then

Parsing:

then receives two callback functions and returns a new promise object. These callback functions correspond to the success callback onFullfilled and the failure callback onRejected, which receive the return value of the promise;
always (finally) takes a callback function and returns a new promise object. The callback function is called after the previous promise has been parsed, i.e., it is called regardless of whether then or catch was called, and takes no arguments.

6. The following logical expression results in false (A)

A:NaN == NaN

B:null == undefined

C:'' == 0

D:true == 1

Parsing:

        NaN == NaN  // false

        (NaN,NaN)  // true

The same NaN is equal, to determine the two NaN is equal to use () before not equal because of the JS design error, now there are ways to judge the

Expansion:

== will do a type conversion and the same value will be true.

=== No type conversion is performed, true if the type and value are the same.

        ()

     === cap (a poem)()The difference between theNaNand signed0The processing:

                  NaN === NaN // false

                  +0 === -0 // true

                  -0 === +0 // true

                  (NaN, NaN) // true

                  (+0, -0) // false

                  (-0, +0) // false

         ('foo''foo');     // true

   (window, window);   // true

   ('foo', 'bar');     // false

   ([], []);           // false

   var foo = { a: 1 };

   var bar = { a: 1 };

   (foo, foo);         // true

   (foo, bar);         // false

   (null, null);       // true

// Special cases

   (0, -0);            // false

   (0, +0);            // true

   (-0, -0);           // true

   (NaN, 0/0);         // true

7. If the output of the following program is false, equation ① can be replaced by (C)

const test = {
  rules: false
};
function Build() {
    = true;
        ①
}
const build = new Build();
();

A:return false;

B:return ;

C:return test;

D: Do nothing

Parsing:

Inside the constructor, if you don't write return, the default is to return the created instance object, but if you add return, if the return is a basic data type, such as, boolean, number, undefined, etc., then the instance object will still be returned, and if the return is an object, the object will be returned, and the original pointing to the actual object this will be invalidated. If the return is an object, the object is returned and the original this pointing to the actual object is invalidated.

Option A:

                const test = {
                          rules: false
                };
                function Build() {
                     = true;
                    return false; // Here when returning a basic type: instance build is an instance object { rules: true}

                    return test ; // when returning an object here: instance build is the object { rules: false}
                }
                const build = new Build();
                ();

8. What is the result of executing the following JS code? (B)

function control(x) {
  if (x == 3) throw new Error("break");
}
function foo(x = 6) {
  return {
    next: () => {
      control(x);
      return {done: !x, value: x && x--};
    }
  }
}
let x = new Object;
x[] = foo;
for (let i of x) (i);

A:6、5、4、3、2、1

B:6, 5, 4, error reporting

C:1、2、3、4、5、6

D:1, 2, error reporting

Parsing:

1. ES6 specifies that the default Iterator interface is deployed on the attributes of a data structure, or that a data structure is considered "traversable" as long as it has attributes (iterable). The property itself is a function, which is the default traverser generation function for the current data structure. Executing this function returns a traverser. As for the property name, it is an expression that returns the iterator property of the Symbol object, which is a predefined, special value of type Symbol, so it goes inside square brackets

2. throw exception message; abort program throws an exception that can be used to abort the program

3. foo is a traverser generator function that aborts traversal and throws an error when it encounters throw.

4. Mr. Nguyen I Phong is also recommended/#docs/iterator

a = parseInt([0,0,1,0,0].join('')+1) The value of a is (C)

A:2        

B:101

C:1001

D:NaN

Parsing:

 var a = parseInt([0,0,1,0,0].join('')+1);
1) First, [0,0,1,0,0].join() results in '00100'
2) Then, '00100'+1 results in '001001'.
3) Finally, parseInt('001001') results in 1001

Exam point 1: the use of array join ()
1) The join() method returns the array as a string when it is not passed a parameter (changing the center bracket of the array to '').
Example: [1,2,3,4,5,6].join() results in '1,2,3,4,5,6'
2) When the join() method is passed parameters, the parameters are used as connectors for each item in the array.
Example 1: [1,2,3,4,5,6].join('') result is '123456' // the joiner is empty
Example 2: [1,2,3,4,5,6].join(' ') results in '1 2 3 4 5 6' // the joiner is a space
Example 3: [1,2,3,4,5,6].join('-') results in '1-2-3-4-5-6' // the joiner is -
Example 4: [1,2,3,4,5,6].join(',') results in '1,2,3,4,5,6' // the joiner is a comma, (note: at this point and join() results in the same)

Exam 2: Arithmetic Operators + - * % /
Addition operation: if one side is a string, then + means string concatenation
If there is no string on either side, then it's a normal addition operation, and anything not of type Number will be implicitly converted to a number first.
Subtract, multiply, divide, take the balance: Number type will be first implicitly converted to digital calculation.
1) String + Numberfor example:'a'+3 catch (a disease) 'a3'
2) Boolean + Numberfor example:false+3 catch (a disease) 3
3) Null + Numberfor example:null+3 catch (a disease) 3(on account ofNumber(null)because of0)
4) undefined + Numberfor example:undefined +3 catch (a disease) NaN(on account ofNumber(undefined)because ofNaN)
5) Boolean + Nullfor example:true+ null catch (a disease) 1
6) Boolean + undefinedfor example:true + undefinde catch (a disease) NaN
Wait ....

Exam point three: the use of parseInt (String, radix)
parseInt() implicitly converts the arguments passed in parentheses to a String first, then to an integer.

() is used to determine the data types, which are.

① basic data types: string number null undefined boolean ② complex data types: object

11. What will be the output of the following JS code (D)?

var a = 'w' 
let obj = {
  a: 'o',
  print: function() {
    ();
  },
  print2: () => { 
    ();
  }
}
let p = ;
let p2 = obj.print2;
();
obj.print2();
p();
p2();

A:o、 undefined、 undefined、undefined

B:o、 w、 undefined、 undefined

C:o、 w、 w、 undefined

D:o、 w、 w、 w

Parsing:

The this of a normal function points to the caller, and the this of an arrow function is aligned with the outermost this.

When obj.print2() is called, this points to obj if it's a normal function; if it's written as an arrow function like the one above, this points to the global object, so you don't get the expected result. This is because objects don't form separate scopes, causing the print2 arrow function to be defined with a global scope.

Mr. Nguyen Yifeng had this to sayES6 Getting Started Tutorial

12. What is the value of len in the final output of the following JS code? (A)

var len = 117;
let func = {
  len: 935,
  showLen: function() {
    ();
  },
  show: function() {
    (function(cb) {
      cb();
    })()
  }
}
();

A:117

B:935

C:undefined

D:null

Parsing:

The immediately executable function expression has this pointing to the global, i.e., window.

var len=117; we can see that this is declared with var, which is a global variable, so the result is printed as 117; if var is changed to let, then the result is undefined

13. setInterval(“alert(welcome)”,1000);  (D)

A:Wait for 1000 seconds and then pop up a dialog box

B:Wait for 1 second and then a dialog box will pop up.

C:Pop up a dialog box every second

D:statement reports an error, syntax is wrong

Parsing:
Grammatical errors:

The first argument to setInterval is a function, not a string;

And welcome needs to be wrapped in quotes, it's a string.

14. A program that executes the following options in strict mode will not throw an exception (D)

A:

uname = 'window';

setTimeout(function(){

()

},1000);

B:

var uname = 'window';

(function(){()}());

C:

function fn(a,a){

(a+a);

}

fn(1,1);

D:

var uname = 'window'

function Person(){();}

var p = new Person();

Parsing:

Option A:
1)uname = 'window' throws an error: Assigning a value to an undeclared variable in strict mode causes a ReferenceError to be thrown, reporting an error. In short, in strict mode variables must be declared with a keyword before they can be used.
2) In strict mode, the this of a normal function points to undefined
3) In strict mode, the this of the immediately executable function points to undefined
4) The window to which the this of the function in setTimeout points in strict mode (unlike normal functions)

Option B:

In strict mode, the immediately executable function's this points to undefinded, so of course it will report an error

Option C:
In strict mode, the parameters of a function cannot have the same name, otherwise an error is reported
In non-strict mode, a function whose arguments have the same name takes the value of the second real parameter.

Option D:
In strict mode: the constructor's this points to undefined, but the constructor's this points to the instantiated object itself.
The this of the object instantiated by the constructor points to the instantiated object itself, and there is no uname attribute under the instantiated object, so you get undefined, and no error is reported.

15. "+new Array(017)" The output of this code is? (B)

A:17

B:NaN

C:15

D:Error

Parsing:

The + operator, when used as a binary operator, has two functions Numeric addition joins strings Numeric addition is nothing to write home about; when joining strings, it converts both arguments to strings before joining them. The + operator, when used as a unary operator, converts the arguments to numbers and returns the result So (+new Array(017)); the output is NaN Other Similarly, the - operator outputs a converted negative number Attached are some other outputs
        (+new Array()); //0 
        (+new Array(0)); //0 
        (+new Array(1)); //0 
(+new Array(2)); //2 or more are NaN //NaN
        (+[]); //0 
        (+[1]); //1 
        (+[1, 2]); //NaN 
        (+[undefined]); //0 
        (+[undefined, undefined]); //NaN

16.The existing function test returns an array num, what is the result if each element of the array is executed iteratively? (D)

function test (){
  var num = []
  var i
  for (i = 0; i < 3; i++) {
      num[i] = function () {
          (i)
      }
  }
  return num
}
test()

A:[0,1,2]

B:[3,3,3]

C:[undefined、undefined、undefined]

D:[ƒ(), ƒ(), ƒ()]

Parsing:

The arrays are [ƒ(), ƒ(), ƒ()].

17. Execute the following program and the output will be (D)

let flag1 = null || undefined;
let flag2 = null && undefined;
if (flag1 === true) ('flag1');
if (flag2 === false) ('flag2');

 A:flag1

B:flag2

C:flag1、flag2

D:Nothing will be output

Parsing:

For ||, the condition returns the value of the first operand if it is true, and the value of the second operand if it is false.

&& is the opposite, returning the value of the second operand if the condition is true, and the value of the first operand if it is false.

|| and && return the value of one of their operands, not the result of a conditional judgment.

So flag1 is undefined and flag2 is null.

18. Perform the following procedure, of the following options, the statement is correct (C)

var obj = {brand:'huawei',price:1999};
(obj,'id',{value:1})
(obj,'price',{configurable:false})
((obj).length); ...①
for (var k in obj){
    (obj[k]); ...②
}
= 999;
delete obj['price']
(obj); ...③

A:①The output result is 3

B:② equation output result for Huawei 1999 1

C: ③ The output is {brand: 'Huawei', price: 999, id: 1}

D: ③ output is {brand: 'Huawei', id: 1}

Parsing:

Why can't the added property be traversed by () nor by for... .in... . in...?
Because: after defineProperty sets a property for an object, the descriptors writable, configurable, and enumberable for that property default to false.
An enumberable of false means that the property is not traversable.

var obj = {brand:'huawei',price:1999};
(obj,'id',{value:1})
// Add an id attribute to obj, at this point obj = { brand:'Huawei',price:1999,id:1 };
// At this point all other attributes of the id are false, not traversable, not writable, not configurable
  
(obj,'price',{configurable:false})
//configurable:false means that price is not configurable and cannot be deleted.
  
((obj).length); 
// The value is ['brand','price'] because the id is not traversable.
  
for (var k in obj){
(obj[k]);//since id is not traversable, the value is Hua is 1999
}
  
= 999;
delete obj['price'] //fails because price is set to configurable:false
(obj);//obj = { brand:'Huawei',price:1999,id:1 }

Knowledge Point Expansion:
for ... .in ... You can iterate over the properties on the prototype object
() Can't iterate over the attributes on the prototype object

1) Attributes describe the object:
JavaScript Provides an internal data structure that describes an object's attributes and controls its behavior, such as whether the attribute is writable, traversable, and so on.
This internal data structure is called the "attributes object".


2) Properties describes the 6 meta-properties of the object:
value: set the value of this attribute, default value is undefined
writable: indicates whether the value of the attribute can be modified, i.e. whether the attribute is writable or read-only, default is true (writable)
enumerable: indicates whether the attribute is traversable, defaults to true (traversable).
configurable: Indicates whether the attribute can be deleted by delete, or whether the attribute's characteristics can be modified, or the attribute can be changed to an accessor attribute, defaults to true (configurable)
get: get is a function that represents the value function (getter) of the attribute, defaults to undefined
set: get is a function that represents a setter for the attribute, defaults to undefined.
configurable, if set to false, prevents certain operations from rewriting the attribute, such as not being able to delete the attribute, and not being able to change the attribute description object of the attribute (except for the value attribute).

19. Based on the following JS code, what would be the output of printing variable a at location A versus printing variable a at location B? (B)

var a = 1;
function test(){
// Position A
class a {}
// Position B
}
test();

A:1、class a {}

B: report error, class a {}

C:Error reporting, error reporting

D:1. Reporting errors

Parsing:

Classes in es6 don't have variable elevation like let const, but both have temporary dead zones. (The actual lifting exists only because of the TDZ, and doesn't get undefined like var, but just throws an error)

        Temporary dead zones:
ES6 explicitly states that if there are let and const commands in the block (the class in this question is also the same as a let const).
This block forms a closed scope from the beginning for variables declared by these commands.
Whenever these variables are used before they are declared, an error is reported.


Temporary dead zones are accessible:/#docs/let

Original code
var a = 1;
function test(){
//(a) Position A
class a {}
// (a) Position B
}
test();
 
In fact the elevated
var a = 1;
function test(){
(a) position A // find a in the test() scope
// is a class but has a TDZ temporary dead zone, accessing it reports an error
    class a {}
(a) Location B //a has been declared created
}
test()

20. Execute the following program and the output will be (C)

class Phone{
  constructor(price){
    = price;
  }
  get price(){
    return 999;
  }
}
var p = new Phone(888);
();

A:999

B:undefined

C:Throw an exception

D:888

Parsing:

This is supposed to be the underlying use of class() : the accessor attribute price is just set to get, set is not by default, so the price attribute is considered as a read attribute

Then on the third line = price it uses set price(set) again, so it will report an error: price is read-only

SOLUTION: get price() {} followed by: set price(value) {} ===> set takes parameters!!!! It returns 999.

21. What will the following JS code output (D)?

var a = 10; 
(function a() {
    a = 20;
    (a); 
})()

A:10

B:20

C:undefined

D:Output the contents of function a

Parsing:

Functions can access their own identifier (function name) in the function body

        Ordinary functions can modify variable names; immediately executable functions cannot modify variable names.

22. What is the output of the following JS code? (C)

function f(x) {
  (x);
  var x = 200;
  (x);
  }
f(a = 100);
(a);

A:undefined、200、undefined

B:100、200、undefined

C:100、200、100

D:undefined、200、100

Parsing:

This is equivalent to creating the global variable a and passing it into the function call

23. Perform the following procedure, the following options, the statement is wrong (B)

function fn(value){
    (arguments instanceof Array); ...①
    ();...②
    (value);...③
    var arr = [...arguments];...④
}

fn(1,2,3);

A:The output of equation ① is false

B: The output of equation ② is 2

C:③The output of the formula is 1.

D:④Equation ④ can use the arguments object to generate a new array

Parsing:

arguments:

  • built-in object
  • has a length and can be traversed
  • No array methods such as pop() push() , arguments are pseudo arrays, not arrays.
  • It is possible to combine ... .arr to extend strings to generate arrays.

        var arr3 = [1, 2, 3]; let arr4 = [...arr3]; (arr4); // 123
     

 function fn(value) {
            (arguments instanceof Array); // false
            (); // 3
(value); // 1 If the real parameter is greater than the formal parameter, match by the number of formal parameters; if the real parameter is less than the number of formal parameters, the extra formal parameter will be undefined, and then the printout will be NaN
            var arr = [...arguments];
            (arr); // [1,2,3]
 }
 fn(1, 2, 3);             

24. Execute the following program to disable the button when the user clicks the button 1 second later, the following options do not meet the requirements (B)

<button> Click </button>
<script>
    var btn = ('button');
</script>

A:

= function(){
    var that = this;
    setTimeout(function(){ = true;},1000)
}

B:

= function(){
    setTimeout(function(){ = true;},1000)
}

C:

= function(){
    setTimeout(()=>{
         = true;
    },1000)
}

D:

= function(){
    setTimeout(function(){ = true;}.bind(this),1000)
}

Parsing:

This pointing problem. A: use that to hold the current button's this B: the callback function executes with this pointing to the window C: the arrow function's this is the outer this, which is the button D: use bind to bind the current this

25. known array arr = [2,20,3,12,9], now to traverse the array, as long as the array exists greater than 10 elements, then the output true, otherwise the output false, then the following options, meet the requirements of (B)

 A:
var res = ((val1,val2)=>{
    return val1 > 10;
})
(res);    

B:
var res = ((val1,val2)=>{
    return val1 > 10;
})
(res);

C:
var res = ((val1,val2)=>{
    return val1 > 10;
})
(res);

D:
var res = ((val1,val2)=>{
    return val1 > 10;
})
(res);

Parsing:

A:[20, 12]

B:true

C:false

D:[false, true, false, true, false]


1. Array method forEach traverses the array

     (function(value, index, array) {
// parameter one is: array element
// parameter two is: index of array element
//parameter three is: current array
     })
// Equivalent to an array traversal for loop with no return value.
2. array method filter filter array
    var arr = [12, 66, 4, 88, 3, 7];

    var newArr = (function(value, index,array) {
// parameter one is: array element
// parameter two is: index of array element
//parameter three is: current array
        return value >= 20;
    });
  
(newArr);//[66,88] // the return value is a new array
3. Array method some
//some Finds if there is an element in the array that satisfies the condition.
    var arr = [10, 30, 4];

    var flag = (function(value,index,array) {
// parameter one is: array element
// parameter two is: index of array element
//parameter three is: current array
        return value < 3;
    });
(flag); //false return value is a boolean value, as long as you find an element that satisfies the condition, you immediately terminate the loop
Difference between some and forEach.

  • If you're looking for the only element in an array, it's more appropriate to use the some method, where return true terminates the traversal.
  • Inside a forEach, return does not terminate the iteration.

4. array method map
//returns an array whose elements are the square roots of the original array

    var numbers = [4, 9, 16, 25];
    function myFunction() {
        x = ("demo")
         = ();
    }
The //map() method returns a new array with the values of the original array elements after the function is called.
The //map() method processes the elements in order of the original array elements.
// Note: map() does not detect empty arrays. // Note: map() does not change the original array.

26. Execute the following program and the equation that will throw a syntax error is (B)

var s = Symbol('key'); ...①
(s + '123'); ...②
var obj = {
    [s]:function(){(1);} ...③
}
var b = (obj); ...④

A:①

B:②

C:③

D:④

Parsing:

A Symbol is essentially a unique identifier that can be used as the name of a unique property of an object so that no one else can rewrite or override the value of the property you set.
caveat
Values cannot operate with values of other types.
Values cannot be mixed with other types of values, otherwise an error will be reported.
If the value is to be used as a property name, then the dot operator can no longer be used, since the dot operator is always followed by a string
4. When using a Symbol value as a property name inside an object, the value must be enclosed in square brackets.

Symbol values cannot operate with values of other types and will report an error.
The new API:() method can return all types of key names, including regular and Symbol key names.

ES6 Getting Started Tutorial