Detailed explanation of two fast writing methods of console. log in vscode

  • 2021-12-04 18:11:59
  • OfStack

(1) Method 1: Define directly in advance in the script tag, only for this html file!


 let add = function(a,b){
 	return a + b;
 };
 console.log(add(20,300));
 
 const { ['log']:C } = console;
 C(add(20,300));

(2) Method 2: Press tab to quickly generate console. log, and the cursor is inside (). Press tab again, and the cursor automatically jumps to the next line!
1. Open the vscode editor and select the file- > Preferences- > User segment, enter javascript. json and press enter to enter.
When we first use it, we will find 1 annotated code as follows:


{
	// Place your snippets for javascript here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
}

2. The interval codes below Example are as follows, and some parameters have the following meanings:
① prefix: the entrance of code shortcut keys, where we can set it according to personal habits. If I set cl, then console. log; can be directly generated with tab;
② body represents the code body:
$1 represents the position where the cursor first appears after the generated code is quickly generated
$2 is written in "console. log ('$1'); "Below, it shows that after console. log () is generated quickly, 1 line is left behind the code, and when tab is pressed again, the cursor jumps to $2 (1 line left)


{
	// Place your snippets for javascript here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	"Print to console": {
		"prefix": "cl",
		"body": [
			"console.log('$1');",
			"$2"
		],
		"description": "Log output to console"
	}
}

Related articles: