Special Exercise 22

Table of contents

1. Multiple choice questions

    1. The result of running the following code snippet is

    2. Execute the following procedures. Among the following options, the wrong statement is ()

2. Programming questions

    1. Return whether the string parameter contains numbers in the form of boolean

    2. Insert the second parameter into the head of the first parameter array and return it as an array


1. Multiple choice questions

1. The result of running the following code snippet is
var obj ={a:1,b:function () {alert(this.a)}}; 
var fun =obj.b; 
fun();

A. pop up a

B. pop up 1

C. Pop up undefined

D. can't see anything

Correct answer: C Your answer: B

Parse:

var b = function(){ alert(this.a); }
obj = { a:1, b:b };// 把函数独立出来
var fun = obj.b;//存储的是内存中的地址 fun();

(1) fun is a reference to obj.b, referring to the b function itself, so fun() is a function call without any decoration, and this points to window

(2) One sentence: whoever the current method belongs to, this is who, fun() belongs to window, so this points to window


2. Execute the following procedures. Among the following options, the wrong statement is ()
class Phone{
  constructor(brand){
    this.brand = brand;
}
  call(){}...①
}
function playGame(){console.log("我可以打游戏")};
function photo(){console.log("我可以拍照")};
console.log(typeof Phone);...②
var p = new Phone('华为');
console.log(p.brand);...③

A. The call method of type ① is defined on the prototype object of the Phone class

B, ②The output result is Object

C, ③The output result is Huawei

D. If you want to add two instance methods of playGame and photo to the class at one time, you can use Object.assign(Phone.prototype,{playGame,photo})

Correct answer: B Your answer: D

Parse:

(1) Option explanation

①Option A: All instance methods of the class are defined on the prototype object of the class. The instance methods defined in the class are equivalent to the methods defined on the prototype object of the class. call() is an instance method

②Option B: The essence of a class is a function . The class in ES6 can be regarded as another way of writing the constructor in ES5, so the output result of the ② formula is  function

③Option C: p is an instance object of the class, which has an attribute brand , and the attribute value is Huawei

④Option D: shallow copy of the object is used , Object.assign(obj, options) is the basic usage method, the object in options will be shallow copied to obj, and obj in the text is the prototype of Phone


2. Programming questions

1. Return whether the string parameter contains numbers in the form of boolean

Parse:

(1) The search() method combines regular expressions

<script>
    let str = 'sdh'
    function _search(str){
        return str.search(/\d/) === -1 ? false:true
    }
    console.log(_search(str));
</script>

(2) Array.from().some() and isNaN() methods

<script>
    let str = ''
    function _search(str){
        return Array.from(str).some(el => isNaN(el) === false)
    }
    console.log(_search(str));
</script>
  • Array.from - create a new shallow copy of an Array instance from an Array-like or Iterable object
  • Array.some - Tests whether an element in the array passes the provided function test, returns a boolean value
  • isNaN —— ①Whether it is a Number type ②Try to convert it to a value ③Check if it is NaN. non-numeric-true; numeric-false

2. Insert the second parameter into the head of the first parameter array and return it as an array

Parse:

(1) unshift() method: Inserting the head of the array will change the original array

<script>
    let array = [1,2]
    let value = 3
    function _unshift(array,value){
        array.unshift(value)
        return array
    }
    console.log(_unshift(array,value));
</script>

(2) reverse() array reverse and push() array addition will change the original array

<script>
    let array = [1,2]
    let value = 3
    function _unshift(array,value){
        array.reverse().push(value)
        return array.reverse()
    }
    console.log(_unshift(array,value));
</script>

(3) The splice() method will change the original array

<script>
    let array = [1,2]
    let value = 3
    function _unshift(array,value){
        array.splice(0,0,value)
        return array
    }
    console.log(_unshift(array,value));
</script>

(4) Destructuring and assigning, creating a new array

<script>
    let array = [1,2]
    let value = 3
    function _unshift(array,value){
        let newArr = [value,...array]
        return newArr
    }
    console.log(_unshift(array,value));
</script>

Guess you like

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