Special Exercise 14

Table of contents

1. Multiple choice questions

    1. What are the results of the following two detections of whether the object constructor has attribute name 1?

    2. What is the len value of the final output of the following JS code?

    3. In JavaScript, which of the following are primitive values?

2. Programming questions

    1. Convert the string parameter to an uppercase string and return

    2. Output the key name of each attribute of the object in the form of an array

    3. Convert the numeric parameter to an object and return it


1. Multiple choice questions

1. What are the results of the following two detections of whether the object constructor has attribute name 1?
1 in Object(1.0).constructor;
Number[1] = 123;
1 in Object(1.0).constructor;

A、false、false

B、false、true

C、true、true

D、true、false

Correct answer: B

Parse:

(1) Object(1.0) is equivalent to new Number(1.0), which instantiates an object of type Number, and Object(1.0).constructor points to the constructor Number that instantiates this object

        The first line 1 in Object(1.0).constructor means to judge whether the Number constructor contains the attribute 1 at this time, and 1 is not on the Number prototype chain, so return false

(2) Number is a constructor and an object, and attributes can be added to objects

        The second line adds a key = 1 attribute to the constructor object in the form of Object[key] = value; and the corresponding value = 123;

        At this time, 1 is on the prototype chain of Number, and the result of the third line 1 in Object(1.0).constructor is true


2. What is the len value of the final output of the following JS code?
var len = 117;
let func = {
  len: 935,
  showLen: function() {
    console.log(this.len);
  },
  show: function() {
    (function(cb) {
      cb();
    })(this.showLen)
  }
}
func.show();

A、117

B、935

C、undefined

D、null

Correct answer: A

Parse:

The this point of the immediately executed function is window (in non-strict mode)

As an anonymous function, when it is called, it is often called directly, so its this is determined, and len takes the len value under the window, which is 117

// 在JavaScript的函数中,this始终指向调用者的上下文环境
var len = 117 // 5. 全局作用域中使用 var 定义的变量默认会成为 window 的属性,及 window.len
let func = {
  len: 935,
  showLen: function () {
    console.log(this.len) // 4. this 此时指向的是 window,所以相当于打印 window.len
  },
  show: function () {
    (function (cb) {
      cb() // 3. cb 相当于 cb.call() 默认没有传入上下文环境时 this 指向全局的 window 对象
    })(this.showLen) // 2. this 是 func 所以传入的是上面定义的 showLen 函数
  }
}

func.show() // 1. 相当于 func.show.call(func),此时 this 是 func对象

3. In JavaScript, which of the following are primitive values?

A、”3”

B、10

C、null

D、[1]

Correct answer: ABC Your answer: BCD

Parse:

(1) In ECMAScript, variables can store two types of values, namely primitive values ​​and reference values

①Original values: simple data segments stored in the stack , their values ​​are stored directly in the location where the variable is accessed

②Reference value: the object stored in the heap

(2) The values ​​in the options are: string, number, null, and the last one is an array , which is an object and a reference value


2. Programming questions

1. Convert the string parameter to an uppercase string and return

Parse:

toUpperCase() method

<script>
    let string = 'sxxhlxxdb'
    function _touppercase(string){
        return string.toUpperCase()
    }
    console.log(_touppercase(string));
</script>

2. Output the key name of each attribute of the object in the form of an array

Example: _keys({name:'nowcoder',age:7}) -> ['name','age']
Note: only consider the case where the object attributes are all primitive data types

Parse:

(1) Method 1: Object.keys(obj)

<script>
    let object = {name:'nowcoder',age:7}
    function _keys(object){
        return Object.keys(object)
    }
    console.log(_keys(object));
</script>

(2) Create an array and push() in each attribute of the for...in traversal object

<script>
    let object = {name:'nowcoder',age:7}
    function _keys(object){
        let arr = []
        for(let i in object){
            arr.push(i)
        }
        return arr
    }
    console.log(_keys(object));
</script>

3. Convert the numeric parameter to an object and return it

示例:typeof number === 'number' -> typeof _numbertoobject(number) === 'object'

Parse:

(1) Method 1: Enhanced writing

<script>
    let number = 123456789
    function _numbertoobject(number){
        return {number}
    }
    console.log(_numbertoobject(number));
</script>

(2) Method 2: new an instance object

<script>
    let number = 123456789
    function _numbertoobject(number){
        return new Object(number)
    }
    console.log(_numbertoobject(number));
</script>

Guess you like

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