After all, the front end is to rely on JS for a meal. I began to dig into the details. In this process, I also reflected on why Java did not learn well. As expected is the foundation is not firm, the earth shook ah, the ancients sincere do not deceive me also!
What are the data types of JS?
Used to always say JS, but even this problem has not done well, give yourself two box in the face……
The data type | The name of the |
---|---|
1. Primitive data types (also called simple data types) | number string boolean symbol undefined null |
2. Complex data types | object |
- Note that array and function are of type object; Symbol is part of ES6.
A few big pit
- Long string inside the pit long string can be used
+
Concatenation, elegant and easy to understand. But you could write it this way
var longStr = 'agggggg\
bgggggg\
cgggggg\
d'// The result is still agGGGGGBGGGGCGGGGGGDCopy the code
The above code does not report an error if \ cannot be followed by any character other than a newline, even if a single space is an error
- The length attribute and character set of a string
- The length*** attribute of a string is immutable. You can write whatever you like. The length attribute remains the same.
- The length*** attribute of a string is immutable. You can write whatever you like. The length attribute remains the same.
Byte Short int Long float Double char Boolean In Java, there are only three methods to determine the length
- The length property of the string
- The length() method of the array
- Size () method of the collection class
- The character set of a string
Because the JavaScript engine (strictly the ES5 specification) does not automatically recognize Unicode characters in the secondary plane (numbered greater than 0xFFFF), all String processing functions encounter such characters and produce incorrect results (see the String objects section in the Library chapter). If you want to do string-related operations, you must determine whether the character falls between 0xD800 and 0xDFFF. Replacement (String. Prototype. Replace), clipping the substring (String. The prototype. The substring, String. Prototype. Slice) and other String manipulation, and similar processing has to do.
The following code
function getSymbols(string) {
var length = string.length;
var index = -1;
var output = [];
var character;
var charCode;
while (++index < length) {
character = string.charAt(index);
charCode = character.charCodeAt(0);
if (charCode >= 0xD800 && charCode <= 0xDBFF) {
output.push(character + string.charAt(++index));
} else{ output.push(character); }}return output;
}
var symbols = getSymbols('𝌆');
symbols.forEach(function(symbol) {
// ...
});
Copy the code
- JavaScript natively provides two Base64 related methods.
Base64 is a representation of binary data based on 64 printable characters. Since 2 to the sixth power is equal to 64, every six bits is a cell corresponding to some printable character. Three bytes have 24 bits, corresponding to four Base64 units, that is, three bytes represent four printable characters. It can be used as the transmission code of E-mail messages. Printable characters in Base64 include the letters A-z, A-z, and the numbers 0-9 for A total of 62 characters, plus two printable symbols that vary in different systems.
Two methods btoa() : string or binary values are converted to Base64 encoding. Atob () : Base64 encoding is converted to the original encoding
var string = 'Hello World! ';
btoa(string) // "SGVsbG8gV29ybGQh"
atob('SGVsbG8gV29ybGQh') / /"Hello World!"
Copy the code
These methods do not work with non-ASCII characters and will result in an error.
btoa('hello')
VM1195:1 Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
at <anonymous>:1:1
Copy the code
To convert non-ASCII characters into Base64 encoding, you must insert a transcoding link and use these two methods
function b64Encode(str) {
return btoa(encodeURIComponent(str));
}
function b64Decode(str) {
return decodeURIComponent(atob(str));
}
b64Encode('hello') / /"JUU0JUJEJUEwJUU1JUE1JUJE"
b64Decode('JUU0JUJEJUEwJUU1JUE1JUJE') / /"Hello"
Copy the code
- The difference between undefined and null? This is an issue left over from history. JS father on Twitter, need FQ to see how should we distinguish
Look at it from two perspectives | why |
---|---|
1. Grammatical norms | A variable has no value, just undefined. In fact, whenever you write a statement on the console, it’s always undefined |
2. Customary understanding | If you have an object and you declare it, but don’t assign it a value, let it be null; If you have a variable that’s not an object and you don’t want to assign it at the moment, make it equal to undefined. |
Var obj = null; var obj = null; Var a = undefinded, it is obvious that you want to declare a non-object and do not want to assign
- An object is a set of key-value pairs, which is a kind of unordered composite data set. Do you use for… *** random *** pit in the order of *** * when in. The following code
var obj = {
'name': 'wushao'.'age'8} :for (var key in obj){
console.log(obj.key)
}
Copy the code
You tried to print ‘Wushao’ and ‘8’, but you got nothing.
- Note that the *** in the [] operator must be a string and the object’s key ***. In this case, key is a string, and it’s obj’s key; You obj. Key is obj[‘key’], obj doesn’t have this key called key. The basic structure of the object {key: value} key must be a string, and valu is one of those seven.
var obj = {
' ': 'frank'No problem}Copy the code
- The question about whether key is in quotes or not. You make sure your key conforms to the identifier specification. You don’t have to add it, but there is a default quotation mark on it. In the same way, if obj[] does not comply with the identifier, it must be quoted.
var person = {
'09a': 'I do not conform to the identifier specification,key must be quoted or an error will be reported',
name: 'I conform to the identifier specification, quote it or not, key must be a string.'
}
Copy the code
– Add a comma to the end of each key-value !!!! The latest ES6 can write commas without error
Anyway, remember a formula
Obj. Name = obj.[‘name’]
- Determine if an attribute is declared using the following formula
if ('a' inWindow) {// variable a declared}else{// Variable A is not declared}Copy the code