// Convert a groovy map to json object Map user = [name:'zdd', age:18, info: [address:'beijing', phone:'123456789']]; println JsonOutput.toJson(user)
// Convert a json object to groovy map def jsonString = '{"name": "John", "age": 30, "city": "New York"}' def jsonSlurper = new JsonSlurper() def jsonObject = jsonSlurper.parseText(JsonOutput.toJson(user)) println jsonObject
// Given a string "feat(configuration): id-xxxx add configuration for user page", please extract the jira id: id-xxx // can you do it with regex? the target string start with ": " and end with " ", return null if not found def str = "feat(configuration): id-xxxx add configuration for user page" def jiraId2 = str =~ /: (.*?) / println jiraId2[0][1]
window.DomContentLoaded - Html文件解析和加载完成(parsed and loaded),且所有标记为defer的js脚本全部下载并执行完成后触发,注意,该事件不会等待其他资源,比如images,subframes,或者标记为async的script下载完成。另外,该事件不会等待stylesheet完成,但是:因为defer脚本会等待stylesheet加载完才执行,而该事件又在defer脚本执行完才触发,所以如果有defer脚本存在的话,那么该事件一定会等待stylesheet加载完才触发。
Posted onEdited onInnode.js Symbols count in article: 128Reading time ≈1 mins.
Famous NPM Packages
cross-env
cross-env is a cross platform solution to setting and using environment variables. It’s available as a command line utility as well as a Node.js module.
Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. By default "node_modules" folder is ignored by transformers.
Posted onEdited onInjavascript Symbols count in article: 288Reading time ≈1 mins.
Optional chaining
Why optional chaining
Have you write the following code before?
1 2 3
if (user && user.address && user.address.street) { console.log(user.address.street); }
With optional chaining operator, you don’t need so many && to get a deeply nested property.
1
console.log(user?.address?.street);
How to use optional chaining
The optional chaining (?.) operator accesses an object’s property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
Access object’s property
1 2 3 4 5 6
const person = { name: 'Philip', };
console.log(person.info.address); // TypeError: Cannot read property 'address' of undefined.(Because, person does not have property 'info') console.log(person.info?.address); // undefined
Call a function
1 2 3 4 5 6 7 8 9 10
functiondoSomething(onContent, onError) { try { // Do something with the data } catch (err) { // Testing if onError really exists if (onError) { onError(err.message); } } }
With optional chaining, you don’t need too check weather onError is defined or not.
1 2 3 4 5 6 7
functiondoSomething(onContent, onError) { try { // Do something with the data } catch (err) { onError?.(err.message); // No exception if onError is undefined } }
Short circuit
When using optional chaining, if the left operand is null or undefined, the expression will not be evaluated, for instance:
1 2 3 4 5
let i = 0; const nums = null; const firstNumber = nums?.[i++]; console.log(x); // 0, since num?. trigger short circuit, [i++] is not evaluated.
?. not work for non-declared root object
Optional chaining can not be used with a non-declared root object, but can be used with a root object that is null or undefined.
1 2 3
console.log(a?.b); // Uncaught ReferenceError: a is not defined console.log(null?.b); // undefined console.log(undefined?.b); // undefined
In javascript, not defined and undefined are two different concepts. See undefined vs not defined
Posted onEdited onInjavascript Symbols count in article: 121Reading time ≈1 mins.
undefined vs not defined
In JavaScript, undefined and not defined are two different concepts.
undefined: a variable has been declared but has not yet been assigned a value.
not defined: a variable has not been declared(not exists).
undefined
1 2
let a; console.log(a); // undefined
not defined
1
console.log(b); // Uncaught ReferenceError: b is not defined
Whenever you try to access a variable that is not declared, JavaScript throws an error: Uncaught ReferenceError: xxx is not defined. This is because variable b is not declared anywhere in the code. but you can still use typeof to check if a variable is defined or not.
typeof
1 2 3
let a; console.log(typeof a); // undefined console.log(typeof b); // undefined, even b is not defined