|
5 | 5 | - [instanceof](#instanceof)
|
6 | 6 | - [scope](#scope)
|
7 | 7 | - [Prototypes](#Prototypes)
|
8 |
| -- [Modular](#Modular) |
9 | 8 |
|
10 | 9 | #### Built-in Types
|
11 | 10 | JavaScript defines seven built-in types, which can be broken down into two categories: `Primitive Type` and `Object`.
|
@@ -75,74 +74,6 @@ let undefined = 1
|
75 | 74 | a === void 0
|
76 | 75 | ```
|
77 | 76 |
|
78 |
| -#### Type conversion |
79 |
| - |
80 |
| -##### Convert to Boolean |
81 |
| - |
82 |
| -Except to `undefined`, `null`, `false`, `NaN`, ````, `0`, `-0`, all other values are converted to `true`, including all objects. |
83 |
| - |
84 |
| -##### Object to basic type |
85 |
| - |
86 |
| -When converting basic types of objects, the first calls `valueOf` and then call `toString`. And these two methods you can rewrite. |
87 |
| - |
88 |
| -```js |
89 |
| -let a = { |
90 |
| - valueOf() { |
91 |
| - return 0 |
92 |
| - } |
93 |
| -} |
94 |
| -``` |
95 |
| - |
96 |
| -##### Four operators |
97 |
| - |
98 |
| -Only when adding, one of which is a string type, the other will be converted to a string type. As long as one of the other operations is a digit, the other party will change to a digit. And addition operation will trigger three types of conversion: the value is converted to the original value、 number & string. |
99 |
| - |
100 |
| -```js |
101 |
| -1 + '1' // '11' |
102 |
| -2 * '2' // 4 |
103 |
| -[1, 2] + [2, 1] // '1,22,1' |
104 |
| -// [1, 2].toString() -> '1,2' |
105 |
| -// [2, 1].toString() -> '2,1' |
106 |
| -// '1,2' + '2,1' = '1,22,1' |
107 |
| -``` |
108 |
| - |
109 |
| -For the addition sign need to pay attention to this expression `'a' + + 'b'` |
110 |
| - |
111 |
| -```js |
112 |
| -'a' + + 'b' // -> "aNaN" |
113 |
| -// because of ++ 'b' -> NaN |
114 |
| -// you may have seen + '1' -> 1 |
115 |
| -``` |
116 |
| - |
117 |
| -##### `==` operators |
118 |
| - |
119 |
| - |
120 |
| - |
121 |
| -Figure above `toPrimitive` is the Object to basic type。 |
122 |
| - |
123 |
| -It is recommended to use `===` to determine two values, but you want to know if a value is or not `null`, you can use `xx == null` to compare. |
124 |
| - |
125 |
| -Here to resolve a topic `[] == ![] // -> true`, follow is the steps that why this expression as `true`: |
126 |
| - |
127 |
| -```js |
128 |
| -// [] convert to true,then revert to false |
129 |
| -[] == false |
130 |
| -// according to 8 |
131 |
| -[] == ToNumber(false) |
132 |
| -[] == 0 |
133 |
| -// according to 10 |
134 |
| -ToPrimitive([]) == 0 |
135 |
| -// [].toString() -> '' |
136 |
| -'' == 0 |
137 |
| -// according to 6 |
138 |
| -0 == 0 // -> true |
139 |
| -``` |
140 |
| - |
141 |
| -##### Comparison operators |
142 |
| - |
143 |
| -1. If it‘s an object, through `toPrimitive` to convert to object. |
144 |
| -2. If it‘s a string, Compare with the `unicode` character index. |
145 |
| - |
146 | 77 | #### New
|
147 | 78 |
|
148 | 79 | 1. Create a new object
|
@@ -597,94 +528,6 @@ var obj = {a: 1, b: {
|
597 | 528 | const clone = await structuralClone(obj);
|
598 | 529 | ```
|
599 | 530 |
|
600 |
| -#### Modular |
601 |
| -
|
602 |
| -In a Babel environment, we can use the modularity of ES6 directly |
603 |
| -
|
604 |
| -```js |
605 |
| -// file a.js |
606 |
| -export function a() {} |
607 |
| -export function b() {} |
608 |
| -// file b.js |
609 |
| -export default function() {} |
610 |
| - |
611 |
| -import {a, b} from './a.js' |
612 |
| -import XXX from './b.js' |
613 |
| -``` |
614 |
| -
|
615 |
| -##### CommonJS |
616 |
| -
|
617 |
| -`CommonJs` is a unique specification of Node, which requires `Browserify` to be used in browser. |
618 |
| -
|
619 |
| -```js |
620 |
| -// a.js |
621 |
| -module.exports = { |
622 |
| - a: 1 |
623 |
| -} |
624 |
| -// or |
625 |
| -exports.a = 1 |
626 |
| - |
627 |
| -// b.js |
628 |
| -var module = require('./a.js') |
629 |
| -module.a // -> log 1 |
630 |
| -``` |
631 |
| -
|
632 |
| -In the above code,`module.exports` & `exports` easily confused,the following is a rough example of implementation |
633 |
| -: |
634 |
| -
|
635 |
| -```js |
636 |
| -var module = require('./a.js') |
637 |
| -module.a |
638 |
| -// Here is actually a layer of immediate execution function, so it will not pollute the global variables |
639 |
| -// the important thing is module that is a variable unique to Node. |
640 |
| -module.exports = { |
641 |
| - a: 1 |
642 |
| -} |
643 |
| -// implementation |
644 |
| -var module = { |
645 |
| - exports: {} // exports a empty object |
646 |
| -} |
647 |
| -// This is why exports and module.exports are used similarly |
648 |
| -var exports = module.exports |
649 |
| -var load = function (module) { |
650 |
| - // Exported structure |
651 |
| - var a = 1 |
652 |
| - module.exports = a |
653 |
| - return module.exports |
654 |
| -}; |
655 |
| -``` |
656 |
| -
|
657 |
| -let talk about `module.exports` & `exports`, |
658 |
| -its practical method is similar, will not have any effect if direct change the values that `exports`. |
659 |
| -
|
660 |
| -
|
661 |
| -The difference between `CommonJS` & the modularity of ES6 is: |
662 |
| -
|
663 |
| -- The former supports dynamic import, that is `require(${path}/xx.js)`, the latter does not currently support, but there are has proposals. |
664 |
| -- The former is a synchronous import, because its use for the Server, the files are all local, and even if the main thread is stuck, the impact of synchronous import is not great. The latter is asynchronous import, because its use for the browser, its need to download the file, if you also use the import will have a great impact on the rendering |
665 |
| -
|
666 |
| -- The former is a value copy when exporting. Even if the exported value is changed, the value that imported will not be changed. Therefore, if you want to update the value, you must need to import it again. However, the latter adopts the real-time binding method. The imported and exported values all point to the same memory address, so the imported value will change follow the export value. |
667 |
| -- The latter will be compiled to `require/exports` to be executed |
668 |
| -
|
669 |
| -##### ADM |
670 |
| -
|
671 |
| -AMD was proposed by `RequireJS` |
672 |
| -
|
673 |
| -```js |
674 |
| -// AMD |
675 |
| -define(['./a', './b'], function(a, b) { |
676 |
| - a.do() |
677 |
| - b.do() |
678 |
| -}) |
679 |
| -define(function(require, exports, module) { |
680 |
| - var a = require('./a') |
681 |
| - a.doSomething() |
682 |
| - var b = require('./b') |
683 |
| - b.doSomething() |
684 |
| -}) |
685 |
| - |
686 |
| -``` |
687 |
| -
|
688 | 531 | #### the differences between call, apply, bind
|
689 | 532 |
|
690 | 533 | Firstly, let’s tell the difference between the former two.
|
|
0 commit comments