@@ -2719,3 +2719,262 @@ function getName(name) {
2719
2719
2720
2720
</p >
2721
2721
</details >
2722
+
2723
+ ###### 87. 输出什么?
2724
+
2725
+ ``` javascript
2726
+ console .log (" I want pizza" [0 ])
2727
+ ```
2728
+
2729
+ - A: ` """ `
2730
+ - B: ` "I" `
2731
+ - C: ` SyntaxError `
2732
+ - D: ` undefined `
2733
+
2734
+ <details ><summary ><b >答案</b ></summary >
2735
+ <p >
2736
+
2737
+ #### 答案: B
2738
+
2739
+ 可以使用方括号表示法获取字符串中特定索引的字符,字符串中的第一个字符具有索引0,依此类推。 在这种情况下,我们想要得到索引为0的元素,字符` 'I' ` 被记录。
2740
+
2741
+ 请注意,IE7及更低版本不支持此方法。 在这种情况下,应该使用` .charAt() `
2742
+
2743
+ </p >
2744
+ </details >
2745
+
2746
+ ---
2747
+
2748
+ ###### 88. 输出什么?
2749
+
2750
+ ``` javascript
2751
+ function sum (num1 , num2 = num1 ) {
2752
+ console .log (num1 + num2)
2753
+ }
2754
+
2755
+ sum (10 )
2756
+ ```
2757
+
2758
+ - A: ` NaN `
2759
+ - B: ` 20 `
2760
+ - C: ` ReferenceError `
2761
+ - D: ` undefined `
2762
+
2763
+ <details ><summary ><b >答案</b ></summary >
2764
+ <p >
2765
+
2766
+ #### 答案: B
2767
+
2768
+ 您可以将默认参数的值设置为函数的另一个参数,只要另一个参数定义在其之前即可。 我们将值` 10 ` 传递给` sum ` 函数。 如果` sum ` 函数只接收1个参数,则意味着没有传递` num2 ` 的值,这种情况下,` num1 ` 的值等于传递的值` 10 ` 。 ` num2 ` 的默认值是` num1 ` 的值,即` 10 ` 。 ``` num1 + num2 ``` 返回` 20 ` 。
2769
+
2770
+ 如果您尝试将默认参数的值设置为后面定义的参数,则可能导致参数的值尚未初始化,从而引发错误。比如:
2771
+ ``` js
2772
+ function test (m = n , n = 2 ) {
2773
+ console .log (m, n)
2774
+ }
2775
+ test () // Uncaught ReferenceError: Cannot access 'n' before initialization
2776
+ test (3 ) // 3 2
2777
+ test (3 , 4 ) // 3 4
2778
+ ```
2779
+
2780
+ </p >
2781
+ </details >
2782
+
2783
+ ---
2784
+
2785
+ ###### 89. 输出什么?
2786
+
2787
+ ``` javascript
2788
+ // module.js
2789
+ export default () => " Hello world"
2790
+ export const name = " Lydia"
2791
+
2792
+ // index.js
2793
+ import * as data from " ./module"
2794
+
2795
+ console .log (data)
2796
+ ```
2797
+
2798
+ - A: ` { default: function default(), name: "Lydia" } `
2799
+ - B: ` { default: function default() } `
2800
+ - C: ` { default: "Hello world", name: "Lydia" } `
2801
+ - D: Global object of ` module.js `
2802
+
2803
+ <details ><summary ><b >答案</b ></summary >
2804
+ <p >
2805
+
2806
+ #### 答案: A
2807
+
2808
+ 使用` import * as name ` 语法,我们将` module.js ` 文件中所有` export ` 导入到` index.js ` 文件中,并且创建了一个名为` data ` 的新对象。 在` module.js ` 文件中,有两个导出:默认导出和命名导出。 默认导出是一个返回字符串“Hello World”的函数,命名导出是一个名为` name ` 的变量,其值为字符串` “Lydia” ` 。
2809
+
2810
+ ` data ` 对象具有默认导出的` default ` 属性,其他属性具有指定exports的名称及其对应的值。
2811
+
2812
+ </p >
2813
+ </details >
2814
+
2815
+ ---
2816
+
2817
+ ###### 90. 输出什么?
2818
+
2819
+ ``` javascript
2820
+ class Person {
2821
+ constructor (name ) {
2822
+ this .name = name
2823
+ }
2824
+ }
2825
+
2826
+ const member = new Person (" John" )
2827
+ console .log (typeof member)
2828
+ ```
2829
+
2830
+ - A: ` "class" `
2831
+ - B: ` "function" `
2832
+ - C: ` "object" `
2833
+ - D: ` "string" `
2834
+
2835
+ <details ><summary ><b >答案</b ></summary >
2836
+ <p >
2837
+
2838
+ #### 答案: C
2839
+
2840
+ 类是构造函数的语法糖,如果用构造函数的方式来重写` Person ` 类则将是:
2841
+
2842
+ ``` javascript
2843
+ function Person () {
2844
+ this .name = name
2845
+ }
2846
+ ```
2847
+
2848
+ 通过` new ` 来调用构造函数,将会生成构造函数` Person ` 的实例,对实例执行` typeof ` 关键字将返回` "object" ` ,上述情况打印出` "object" ` 。
2849
+
2850
+ </p >
2851
+ </details >
2852
+
2853
+ ---
2854
+
2855
+ ###### 91. 输出什么?
2856
+
2857
+ ``` javascript
2858
+ let newList = [1 , 2 , 3 ].push (4 )
2859
+
2860
+ console .log (newList .push (5 ))
2861
+ ```
2862
+
2863
+ - A: ` [1, 2, 3, 4, 5] `
2864
+ - B: ` [1, 2, 3, 5] `
2865
+ - C: ` [1, 2, 3, 4] `
2866
+ - D: ` Error `
2867
+
2868
+ <details ><summary ><b >答案</b ></summary >
2869
+ <p >
2870
+
2871
+ #### 答案: D
2872
+
2873
+ ` .push ` 方法返回数组的长度,而不是数组本身! 通过将` newList ` 设置为` [1,2,3].push(4) ` ,实际上` newList ` 等于数组的新长度:` 4 ` 。
2874
+
2875
+ 然后,尝试在` newList ` 上使用` .push ` 方法。 由于` newList ` 是数值` 4 ` ,抛出TypeError。
2876
+
2877
+ </p >
2878
+ </details >
2879
+
2880
+ ---
2881
+
2882
+ ###### 92. 输出什么?
2883
+
2884
+ ``` javascript
2885
+ function giveLydiaPizza () {
2886
+ return " Here is pizza!"
2887
+ }
2888
+
2889
+ const giveLydiaChocolate = () => " Here's chocolate... now go hit the gym already."
2890
+
2891
+ console .log (giveLydiaPizza .prototype )
2892
+ console .log (giveLydiaChocolate .prototype )
2893
+ ```
2894
+
2895
+ - A: ` { constructor: ...} ` ` { constructor: ...} `
2896
+ - B: ` {} ` ` { constructor: ...} `
2897
+ - C: ` { constructor: ...} ` ` {} `
2898
+ - D: ` { constructor: ...} ` ` undefined `
2899
+
2900
+ <details ><summary ><b >答案</b ></summary >
2901
+ <p >
2902
+
2903
+ #### 答案: D
2904
+
2905
+ 常规函数,例如` giveLydiaPizza ` 函数,有一个` prototype ` 属性,它是一个带有` constructor ` 属性的对象(原型对象)。 然而,箭头函数,例如` giveLydiaChocolate ` 函数,没有这个` prototype ` 属性。 尝试使用` giveLydiaChocolate.prototype ` 访问` prototype ` 属性时会返回` undefined ` 。
2906
+
2907
+ </p >
2908
+ </details >
2909
+
2910
+ ---
2911
+
2912
+ ###### 93. 输出什么?
2913
+
2914
+ ``` javascript
2915
+ const person = {
2916
+ name: " Lydia" ,
2917
+ age: 21
2918
+ }
2919
+
2920
+ for (const [x , y ] of Object .entries (person)) {
2921
+ console .log (x, y)
2922
+ }
2923
+ ```
2924
+
2925
+ - A: ` name ` ` Lydia ` and ` age ` ` 21 `
2926
+ - B: ` ["name", "Lydia"] ` and ` ["age", 21] `
2927
+ - C: ` ["name", "age"] ` and ` undefined `
2928
+ - D: ` Error `
2929
+
2930
+ <details ><summary ><b >答案</b ></summary >
2931
+ <p >
2932
+
2933
+ #### 答案: A
2934
+ ` Object.entries() ` 方法返回一个给定对象自身可枚举属性的键值对数组,上述情况返回一个二维数组,数组每个元素是一个包含键和值的数组:
2935
+
2936
+ ` [['name','Lydia'],['age',21]] `
2937
+
2938
+ 使用` for-of ` 循环,我们可以迭代数组中的每个元素,上述情况是子数组。 我们可以使用` const [x,y] ` 在` for-of ` 循环中解构子数组。 ` x ` 等于子数组中的第一个元素,` y ` 等于子数组中的第二个元素。
2939
+
2940
+ 第一个子阵列是` [“name”,“Lydia”] ` ,其中` x ` 等于``name“` ,而 ` y` 等于 ` Lydia`。
2941
+ 第二个子阵列是` [“age”,21] ` ,其中` x ` 等于``age“` ,而 ` y` 等于 ` 21`。
2942
+
2943
+ </p >
2944
+ </details >
2945
+
2946
+ ---
2947
+
2948
+ ###### 94. 输出什么?
2949
+
2950
+ ``` javascript
2951
+ function getItems (fruitList , ... args , favoriteFruit ) {
2952
+ return [... fruitList, ... args, favoriteFruit]
2953
+ }
2954
+
2955
+ getItems ([" banana" , " apple" ], " pear" , " orange" )
2956
+ ```
2957
+
2958
+ - A: ` ["banana", "apple", "pear", "orange"] `
2959
+ - B: ` [["banana", "apple"], "pear", "orange"] `
2960
+ - C: ` ["banana", "apple", ["pear"], "orange"] `
2961
+ - D: ` SyntaxError `
2962
+
2963
+ <details ><summary ><b >答案</b ></summary >
2964
+ <p >
2965
+
2966
+ #### 答案: D
2967
+
2968
+ ` ... args ` 是剩余参数,剩余参数的值是一个包含所有剩余参数的数组,** 并且只能作为最后一个参数** 。上述示例中,剩余参数是第二个参数,这是不可能的,并会抛出语法错误。
2969
+
2970
+ ``` javascript
2971
+ function getItems (fruitList , favoriteFruit , ... args ) {
2972
+ return [... fruitList, ... args, favoriteFruit]
2973
+ }
2974
+
2975
+ getItems ([" banana" , " apple" ], " pear" , " orange" )
2976
+ ```
2977
+
2978
+ 上述例子是有效的,将会返回数组:` [ 'banana', 'apple', 'orange', 'pear' ] `
2979
+ </p >
2980
+ </details >
0 commit comments