Special Exercise 17

Table of contents

1. Multiple choice questions

    1. The following description about JavaScript modularization is wrong

    2. Execute the following code, the output result is ()

    3. The following JavaScript code returns false

    4. Execute the following procedures. Among the following options, the correct statement is ()


1. Multiple choice questions

1. The following description about JavaScript modularization is wrong

A. Modularization is beneficial to manage dependencies between modules, and more dependent on module maintenance

B. Mainstream modularization includes CommonJS, AMD, CMD, etc.

C. Sea.js follows the AMD specification, and RequireJS follows the CMD specification

D. AMD advocates relying on the front, and CMD advocates relying on the nearest

Correct answer: C

Parse:

(1) AMD is the abbreviation of "Asynchronous Module Definition", which refers to the asynchronous module definition

①It loads modules asynchronously, and the loading of modules does not affect the operation of subsequent statements

② All the statements that depend on this module are defined in a callback function, and the callback function will not run until the loading is complete

③ AMD also uses the require() statement to load modules, but unlike CommonJS, there are two Javascript libraries that implement the AMD specification: require.js and curl.js

④ More reference links

Javascript modular programming (2): AMD specification - Ruan Yifeng's weblog http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html (2) AMD and CMD

① Nature

AMD is the standardized output of RequireJS's module definition during the promotion process
CMD is the standardized output of SeaJS's module definition during the promotion process
② Difference

Dependent modules: AMD is executed in advance , and the pre-dependence is highly recommended ; CMD is delayed execution , and the near-dependence is highly recommended

(3) Example: Visually check the proximity and preposition of dependencies

<script>
    // CMD
    define(function(require,exports,module){
        var a = require('./a')
        a.doSomething()//a执行某些事情
        var b = require('./b')//依赖可以就近
        b.doSomething()//b执行某些事情
    });
    // AMD
    define(['./a','./b'],function(a,b){//依赖必须一开始就写好
        a.doSomething()//a执行某些事情
        b.doSomething()//b执行某些事情
    })
</script>

2. Execute the following code, the output result is ()
var val = 12;
function fun1(){
    console. log(val);
    var val = 20;
    console.log(val);
}
fun1();

A、12 20

B、12 12

C、undefined 20

D、20 undefined

Correct answer: C Your answer: A

Parse:

(1) Option explanation

① When local variables and global variables exist at the same time, the priority of local variables is higher

②In the function fun1, due to variable promotion , var will be used to declare the local variable val first, and the assignment of the variable will not be promoted together, and then use console.log() to output val. Since the val variable has been declared but not assigned, Therefore, the output result is undefined

③ Then assign a value to val, its value is 20, and finally output val again, and the output result is 20

(2) Code explanation: Due to the variable promotion of var, the code of the title is relative to the following code

var val = 12;
function fun1(){
    var val
    console. log(val);
    val = 20;
    console.log(val);
}
fun1();

3. The following JavaScript code returns false

A、var test=1;typeof test=='number';

B、var test=1.2;typeof test=='float';

C、var test=undefined;typeof test=='undefined';

D、var test={};typeof test=='object';

E、var test='4399'-0;typeof test=='number';

F、var test=null;typeof test=='object';

Correct answer: B Your answer: E

Parse:

(1) Types that typeof can output

type explain
undefined value is undefined
boolean Boolean value
string string
number value
object object or null
function function
Symbol ES6 new type

(2) Option explanation

①Option A: typeof tset returns 'number', 'number' == 'number', returns true 

②Option B: typeof tset returns 'number', 'number' == 'float', returns false 

③Option C: typeof tset returns 'undefined', 'undefined' == 'undefined', returns true

④Option D: typeof tset returns 'object', 'object' == 'object', returns true

⑤Option E: typeof tset returns 'number', 'number' == 'number', returns true

  • The js interpreter will call Number() to convert the string '4399' to 4399, and then calculate
  • If it is  '+' , string splicing ; if the operand has an object, value, or Boolean value, call the toString() method to continue string splicing.
  • If  '-' , calculate according to the value

⑥Option F: typeof tset returns 'object', 'object' == 'object', returns true


4. Execute the following procedures. Among the following options, the correct statement is ()
class Dog{
  static dog(){
    console.log(this); ...①
    }
  bark(){
    console.log('狗会叫');
    }
}
var dog = new Dog();

A. ① this refers to the instance object of the class

B. To call the dog method, either Dog.dog() or dog.dog() can be used

C. To call the bark method, you can only use dog.bark(), not Dog.bark()

D. In a class, static methods and non-static methods are not allowed to have the same name

Correct answer: C Your answer: A

Parse:

(1) The dog method is a static method, and the bark method is an instance method

  • The static method itself cannot be called using the instance object, so this will not point to the instance object
  • Static methods can only be called by the class , and instance methods can only be called by the instance object

(2) Option Analysis

①Option A: this in static dog() refers to the class Dog itself, and this in bark refers to the instantiated object of the class

②Option B: Call the static dog() method, only Dog.dog() can be used; dog.dog() cannot be used; 

③Option C: If there is no static in front of the method in the class, the method can only be called by the instantiated object of the class, not by the class itself

④Option D: Since the caller of the static method inside the class can only be the class, the caller of the instance method can only be the instance object, which will not cause ambiguity when calling, so the same name is allowed

Guess you like

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