Special Exercise 23

Table of contents

1. Multiple choice questions

    1. What is the type of return value of document.getElementById() in JavaScript?

    2. Among the following options, one that does not belong to the way of JavaScript inheritance is ()

    3. Which of the following expressions evaluate to 0?

2. Programming questions

    1. Return the first index value of the second parameter in the first parameter array in the form of numbers


1. Multiple choice questions

1. What is the type of return value of document.getElementById() in JavaScript?

A、Array

B、Object

C、String

D、Function

Correct answer: B

Parse:

(1) The difference between getElementById with brackets and without brackets :

①With brackets: document.getElementById('id');  returns an Element object , and the return value type is Object

        getElementById is a function under the document object, followed by () is to call the getElementById function to get the return value of the function, and the return value of the function is an element node object

②No brackets: document.getElementById;  returns a function , the return value type is Function

getElementById is a function under the document object. Function usage: Pass an id to the function, and this function will return the dom node         corresponding to the first id on the page, which is an element object, to us. If there is no dom node corresponding to the id , returns null

(2) The difference between document.getElementById() and document.getElementsByName():

①document.getElement ById () returns a single element, and document.getElements ByName () returns an array of elements

②Because the name attribute in a document may not be unique, (such as radio buttons in HTML forms usually have the same name attribute), so the getElementsByName() method returns an array of elements, not an element

(3) Common methods of obtaining dom nodes under the document object

  • document.getElementsByClassName(): Returns a collection of elements of all specified class names in the document as a NodeList object (pseudo-array)
  • document.getElementsByName(): returns a collection of objects with the specified name
  • document.getElementsByTagName(): returns a collection of objects with the specified tag name
  • document.querySelectorAll(): Returns a list of all element nodes that match the CSS selector in the document , a new method introduced in HTML5

Note: The above four returns are all collections , all of which are getElements , not getElement. Each item in the array is the corresponding dom node

document.getElementById(): Returns a reference to the first object with the specified id

document.querySelector(): Returns the first element in the document that matches the specified CSS selector

Note: the above two return a single dom node object


2. Among the following options, one that does not belong to the way of JavaScript inheritance is ()

A. Prototype chain inheritance

B. Constructor inheritance

C. Combined inheritance

D. Associative inheritance

Correct answer: D Your answer: C

Parse:

(1) The common inheritance methods of js include prototype chain inheritance, borrowing constructor inheritance, combination inheritance, prototype inheritance, parasitic inheritance, parasitic combination inheritance, and ES6’s new class inheritance, but does not include associative inheritance

(2) A detailed description of these inheritances

https://www.cnblogs.com/Leophen/p/11401734.html


3. Which of the following expressions evaluate to 0?

A、(()=>{}).length

B、1 & 2

C、+[]

D、[1,2,-3].reduce((a, b) => a - b, 0)

Correct answer: ABCD Your answer: ACD

Parse:

(1) Option A: (()=>{}).length; Get the number of formal parameters of the method , and the formal parameter is 0

  • If it is ((a)=>{}).length, the formal parameter has one, so the length of the function is 1
  • If it is ((a, b) => {}).length, there are two formal parameters , so the length of the function is 2

(2) Option B: bitwise AND operation, only 1 is 1, otherwise return 0, 1=0001, 2=0010, 1&2 result is 0000, converted to decimal is 0

(3) C option: +[] implicit type conversion , + will make [] implicitly converted to Number, because [] is an object, so toPrimitive->valueOf->toString is a '' string, Number('') get 0 

(4) D option: reduce accumulator , executes a reduce function on each element in the array, and summarizes its results into a single return value. a is the return value of the cumulative callback of the accumulator, b is each element of the array, and the initial value is passed in 0->0-(1)->(-1)-2->(-3)-(-3) ->0

For an example description of reduce(), see this article of the editor: ES6 Articles (Part 1)


2. Programming questions

1. Return the first index value of the second parameter in the first parameter array in the form of numbers

Note: Returns -1 if the target value does not exist in the array

Parse:

(1) indexOf () method

<script>
    let array = [35,24,88,23,67,35,63,24,72]
    value = 24
    function _indexof(array,value){
        return array.indexOf(value)
    }
    console.log(_indexof(array,value));
</script>

(2) findIndex() method

<script>
    let array = [35,24,88,23,67,35,63,24,72]
    value = 24
    function _indexof(array,value){
        return array.findIndex(e=>e === value)
    }
    console.log(_indexof(array,value));
</script>

(3) for () loop

<script>
    let array = [35,24,88,23,67,35,63,24,72]
    value = 24
    function _indexof(array,value){
        for(let i in array){
            if(array[i] === value){
                return +i
            }
        }
        return -1
    }
    console.log(_indexof(array,value));
</script>

Guess you like

Origin blog.csdn.net/qq_51478745/article/details/131627497