Special Exercise 29

Table of contents

1. Multiple choice questions

    1. What happens when the name attribute in obj is modified?

    2. After the following code is executed, the results of ax and bx are ()

    3. In ECMAScript6, the status of Promise is ()

    4. Which of the following methods can detect that a js object is an array type in the same window? ( )


1. Multiple choice questions

1. What happens when the name attribute in obj is modified?
let obj = {name: '222'};
let _name = obj.name;
Object.defineProperty(obj, 'name', {
  get() {
    return _name;
  },
  set(newVal) {
    console.log(newVal, _name);
    _name = newVal;
  }
})
obj.name = '11';

A. Print 11

B. Print 222

C. Print 11 and 222

D. print nothing

Parse:

(1) Here, the object's native method is used to monitor the name attribute of obj , mainly to print out the new value and the old value at the same time when the name attribute is modified.

(2) Three parameters of object.defineProprty()

①The object being operated

②The properties and methods of the object to be manipulated , when accessing this property, will call get(), such as console.log(); and when modifying this property, will call set(), modifying obj in the last line .name This attribute will call set(), first print the value newVal received by set(), then print the previous value, and finally assign the value of newVal to _name;

③A callback function

(3) Code comments

<script>
    let obj = { name: '222' };
    let _name = obj.name;//相当于_name='222' 
    Object.defineProperty(obj, 'name', {
        get() {
            return _name;
        },
        set(newVal) {
            console.log(newVal, _name);
            _name = newVal;
        }//重新定义name属性 
    })
    obj.name = '11';//调用了name的set方法 所以先输出'11'再输出'222'
</script>

2. After the following code is executed, the results of ax and bx are ()
function A(x){
  this.x = x;
}
A.prototype.x = 1;
 
function B(x){
  this.x = x;
}
B.prototype = new A();
var a = new A(2), b = new B(3);
delete b.x;

A、2, 3

B、2, 1

C、2, undefined

D. Other items are wrong

Correct answer: C

Parse:

Knowledge points: prototype, new operator

(1) Object a : It has attribute x itself, and the attribute value is 2. At the same time, its prototype object also has attribute x, and the attribute value is 1

(2) Object b : It also has attribute x at the time of initialization, and the attribute value is 3. At the same time, its prototype object is an instance of function A , which also has attribute x. Since there is no parameter passed , the value of attribute x is undefined

① When delete bx, the x attribute of object b itself is deleted , but the x attribute on its prototype object will not be deleted

②According to the scope chain rules of object property search, when accessing object properties, it will first search for the properties of the object itself , if it does not exist , it will continue to search on its prototype object , so the return result of ax is 2, and the return of bx The result is undefined

(3) Code Analysis

<script>
    function A(x) {
        this.x = x;
    }
    A.prototype.x = 1;//将A的prototype.x赋值为1
    //prototype:是JS中的原型链机制,当读取A中的x时,如果A中找不到,则通过原型链去查找x
    //去new一个对象时会继承这个原型链

    function B(x) {
        this.x = x;
    }
    B.prototype = new A();
    //将B的原型链指向A
    //B.prototype = { x: undefined }

    var a = new A(2), b = new B(3);
    // a.x可以直接取到结果
    //b = { x: 3}
    //b.__proto__ = { x: undefined }

    delete b.x;
    // b.x原本为3后来被删除,则进入原型链查找,结果为undefined
    //b = { }
    //.b__proto__ = { x: undefined}
    console.log(a.x);
    console.log(b.x);
</script>

①new operator: new operator - JavaScript | MDN

②Inheritance and the prototype chain: Inheritance and the prototype chain - JavaScript | MDN

③delete operator: delete operator - JavaScript | MDN


3. In ECMAScript6, the status of Promise is ()

A、Pending

B、Pause

C、Resolved

D、Rejected

Correct answer: ACD Your answer: CD

Parse:

(1)Promise

① Three states pending fulfilled rejected

②Two processes pending->fulfilled(resolve) pending->rejected(reject)

③A method then

(2) Reference documents

ES6 Getting Started Tutorial


4. Which of the following methods can detect that a js object is an array type in the same window ? ( )

A、Array.isArray()

B、instanceof

C、typeof

D、Object.prototype.toString.call()

Correct answer: ABD Your answer: C

Parse:

(1) option

①Option A: Array is the native object of js, it has a static method: Array.isArray() , which can determine whether the parameter is an array

②Option B: The instanceof operator returns a Boolean value indicating whether the object is an instance of a constructor

③Option C: typeof judges  object, array, and null  variables all return  object

④D option: Object.prototype.toString() is an instance method of the Object object. By default (that is, the method is not rewritten), the type string of the return parameter

(2) Code output

<script>
    let arr = [1,2,3]
    console.log(Array.isArray(arr));//true
    console.log(arr instanceof Array);//true
    console.log(typeof arr);//object
    console.log(Object.prototype.toString.call(arr).slice(8,-1));//Array
</script>

Guess you like

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