To the reader
This article is the fifth in the series of "A JSer's Dart Learning Log". This series of articles mainly aims to explore the similarities and differences between JS and Dart, while reviewing and consolidating JS while smoothly transitioning to the Dart language.
Unless otherwise specified, the JS in this article contains all the features from ES5 to ES2021, and the Dart version is 2.0 or higher.
Since the author is still a beginner to Dart, his understanding may be superficial and one-sided. It is inevitable that there will be some omissions in the generalization of knowledge. This series of articles was first published on the Sifu platform, and the errata and new content are also limited to Sifu. If you read this article elsewhere, please think about it so as not to see the unrevised content.
about
The five basic types of ES5 (
Number
,String
,Null
,Undefined
,Boolean
) are common interview questions, and ES2015 bringsSymbol
and 83f28c5 and ES2015 that are available forBigInt
In terms of data structure, JSers could onlyObject
andArray
, which was often accompanied by some unwritten constraints, until ES6 broughtMap
andSet
. In terms of built-in data types, Dart's starting point is obviously higher than JS.JS is an explanatory language, and its language standard restricts the running host (although the manufacturer may not follow this standard), so the memory usage can be clarified in the JS standard. For example, in addition to variable references, certain These data are also stored in the stack memory, these data are the so-called "basic type" data.
And Dart often needs to be compiled into other languages, and the behavior of explicitly restricting the host through the standard will only increase the workload. This may be the reason why Dart does not clearly specify the "basic type".But this does not prevent us from comparing Dart's functionally similar types with JS's basic types.
One, Number
Common ground
- Use decimal number literal declaration;
- Use hexadecimal literal declaration;
Use scientific notation to declare:
/****** Both JS and Dart ******/ var dec = 42; var hex = 0X2A; var sic = 4e2;
difference
1. Dart has no binary literals
- JS uses
0b
declare binary literals (b
can also be capitalized); Dart does not support binary literals:
> /* JS */ | // Dart > var bin = 0b101010; | // var bin = 0b101010; > | // 去掉上面的注释,将无法通过编译
2. int
and double
- The
Number
types of JS are stored and calculated in double-precision floating-point types; Dart's
Number
divided into integerint
and double-precision floating-point typedouble
to cope with different representation and calculation requirements:> /* JS */ | // Dart > var i = 1; | var i = 1; > var d = 1.1; | var d = 1.1; > console.log(typeof i); | print(i.runtimeType); > console.log(typeof d); | print(d.runtimeType); > // number | // int > // number | // double
However, using Dartpad to measure:
If there is only0
after the decimal point,d.runtimeType
isint
;
However, ifd
declarednum
orint
, and asdouble
argument types, it will not compile.
Does it mean that Dart designers are overconfident in their type inference functions?
Also in Dart:
If the two numbers are subtracted to 0, then when the==
is used to determine the equality, the return istrue
;double
andint
, and the result type isdouble
.
3. Create numbers from other types
Number
constructor in JS to create a number (but you cannot use thenew
keyword). If the parameter is an expression of another type, the expression will be converted to a number type;In Dart, only
num.parse
/num.tryParse
can be used to convert a string to a number:var a = Number('2'); | var a = num.parse('2');
parse
will throw an error when encountering a string that cannot be converted to a number;tryParse
null
when it encounters a string that cannot be converted into a number;
Both will perform strict parameter type matching.
4. Special values
- Corresponds to
NaN
JS, but does not have the literalNaN
- The Dart
double.maxFinite
corresponding to the JSNumber.MAX_VALUE
(but the particular value depends on the host environment); - The DART
double.minPositive
corresponding to the JSNumber.MIN_VALUE
; - The DART
double.infinity
corresponding to the JSNumber.POSITIVE_INFINITY
; - The DART
double.negativeInfinity
corresponding to the JSNumber.NEGATIVE_INFINITY
.
I don't know if they are common or different.
The Dartint
type like the JSBigInt
, above these values, which - all! No! Have!
Two, Boolean
VS Boolean type
Common ground
- Use literal declarations.
difference
1. No implicit type conversion and versatile constructor
- In JS, you can use
Boolean
should be used. The value of these expressions will be converted to a Boolean value during operation. This process involves a very complex truth table, which has the same effect. CallBoolean(exp)
; - Dart used in
bool
where this type must use the type andbool
not an executable or a function, and no other type of value conversion method.
Therefore, when judging true or false anywhere, you must explicitly convert the expression to a Boolean type.
Three, String
VS Strings
Common ground
- You can use single and double quotes (ie
"
or'
) to declare the string literal; - Use backslashes (
\
) to escape special characters; Use the plus sign (
+
) to concatenate strings:/****** Both JS and Dart ******/ var stringA = 'This is a string'; var stringB = 'This\'s a string'; var stringC = stringA + stringB;
the difference
1. Multi-line string
- ES5 does not have a multi-line string literal. Use
\n
represent the newline character. ES6 adds backquote (`
) syntax, and you can directly enter a newline by pressing Enter; Dart uses triple quotation marks (
'''
or"""
) syntax to represent multi-line strings:> /* JS */ | // Dart > const a = | const a = > `Line 1st | '''Line 1st; > Line 2nd`; | Line 2nd'''; > console.log(a); | print(a); > // Line 1st \nLine 2nd | // Line 1st \nLine 2nd
2. String template
- ES5 also does not have a string template function. Developers usually use string splicing or fragment replacement to embed expressions into strings. In the new backtick syntax of ES6, you can use
${exp}
to string to achieve the same effect; All strings in Dart can use
${exp}
or$varName
to embed the value of an expression/variable in a string:> /* JS */ | // Dart > const val = 'expression'; | const val = 'expression'; > var oldVer = 'This is ' + val; | const oldVer = "This is $val"; > const newVer = `This is ${val}`; | const newVer = 'This is ${val}'; > console.log(oldVer === newVer); | print(oldVer == newVer); > // true | // true
3. No implicit conversion and constructor
- In JS, we can use a string to concatenate any expression to get a new string. The
.toString
method of the expression will be executed by default, and you can also useString(exp)
convert the expression to a string; - In Dart, strings cannot be connected with other types of expressions, and
Strings
is not a callable function or constructor.
With the help of operator overloading, we should be able to implement implicit type conversion in Dart. The only question is: do we need this?
4. raw
string
- JS does not have the
raw
string, and there is no similar concept; In Dart, add a
r
before the quotation mark to indicate that the slash in this string is not an escape character:// Dart only const raw = r'In a raw string, not even \n gets special treatment.';
\n
here will not be treated as a newline character.
Of course, as the most common basic type in UI programming, string has many convenient methods and rich usage. It is expected that listing these similarities and differences will take up a lot of space.
Four, Symbol
Common ground
Use
Symbol(String t)
create aSymbol
:/****** Both JS and Dart ******/ const symbol = Symbol('symbol');
difference
1. Equality of different Symbol instances
- Different Symbols in JS are not equal, even if the strings passed in when they are created are the same;
Symbol
Dart are the same, the resulting instances are equal:> /* JS */ | // Dart > const a = Symbol('symbol'); | const a = Symbol('symbol'); > const b = Symbol('symbol'); | const b = Symbol('symbol'); > console.log(a == b); | console.log(a == b); > // false | // true
2. #
No. 0616a7f83f366b
In Dart, you can use
#
quickly declare a constantSymbol
:#symbol; print('This is ${#symbol}'); // This is Symbol("symbol")
Notice
- You should also bring
#
when you use it; - Statement
Symbol
clear compile-time constants, and therefore can no longer usevar
,const
,late
keywords such, can not be usedSymbol
declare its type.
- You should also bring
Five, Null
VS Null
&& Undefined
- There are two types of null values in JS:
undefined
andnull
. The types areundefined
andNull
respectively. The former is often used as the default value of an unassigned variable, while the latter is often used as the return value when some interfaces have no target value; In Dart, only the
null
null value is used as the default value of the unassigned variable. In the null safe (sound null safety
) mode, theType?
syntax must be used to assign a value:String? someString = null; // 空安全模式下,需要使用`?`,才能为变量声明初值
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。