mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 18:57:12 +00:00
show line numbers in conde snippets
This commit is contained in:
parent
cd1df0e376
commit
255a68d673
82 changed files with 1249 additions and 1251 deletions
|
@ -1,4 +1,4 @@
|
|||
# JavaScript
|
||||
# JavaScript
|
||||
|
||||
## Basics
|
||||
|
||||
|
@ -11,14 +11,14 @@
|
|||
|
||||
### Comments
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
//single line comment
|
||||
/*multiline comment*/
|
||||
```
|
||||
|
||||
### File Header
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
/**
|
||||
* @file filename.js
|
||||
* @author author's name
|
||||
|
@ -32,7 +32,7 @@
|
|||
|
||||
If located at the top of the script the whole script works the “modern” way (enables post-ES5 functionalities).
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
"use strict"
|
||||
|
||||
// script contents
|
||||
|
@ -42,7 +42,7 @@ If located at the top of the script the whole script works the “modern” way
|
|||
|
||||
Interrupts script execution until closure, **to be avoided**
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
alert("message");
|
||||
```
|
||||
|
||||
|
@ -69,7 +69,7 @@ Variable names can only contain numbers, digits, underscores and $. Variable nam
|
|||
Variable declared with `let` are in local to the code block in which are declared.
|
||||
Variable declared with `var` are local only if declared in a function.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
function func(){
|
||||
variable = value; // implicitly declared as a global variable
|
||||
var variable = value; // local variable
|
||||
|
@ -99,7 +99,7 @@ Hard-coded values are UPPERCASE and snake_case, camelCase otherwise.
|
|||
|
||||
Only numeric type is `number`.
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let number = 10; //integer numbers
|
||||
number = 15.7; //floating point numbers
|
||||
number = Infinity; //mathematical infinity
|
||||
|
@ -115,7 +115,7 @@ Mathematical expression will *never* cause an error. At worst the result will be
|
|||
|
||||
### String data type
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let string = "text";
|
||||
let string$ = 'text';
|
||||
let string_ = `text ${expression}`; //string interpolation (needs backticks)
|
||||
|
@ -137,7 +137,7 @@ Property access is unpredictable:
|
|||
|
||||
If the parameters to slice are negative, they reference the string from the end. Substring and substr doesn´t.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
string.slice(begin [, end]);
|
||||
string.substring(from [, to]);
|
||||
string.substr(start [, length]);
|
||||
|
@ -145,27 +145,27 @@ string.substr(start [, length]);
|
|||
|
||||
### Boolean data type
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let boolean = true;
|
||||
let boolean_ = false;
|
||||
```
|
||||
|
||||
### Null data type
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let _ = null;
|
||||
```
|
||||
|
||||
### Undefined
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let $; //value is "undefined"
|
||||
$ = undefined;
|
||||
```
|
||||
|
||||
### Typeof()
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
typeof x; //returns the type of the variable x as a string
|
||||
typeof(x); //returns the type of the variable x as a string
|
||||
```
|
||||
|
@ -176,7 +176,7 @@ It is a special value with a separate type of its own. So, again, this is an err
|
|||
|
||||
### Type Casting
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
String(value); //converts value to string
|
||||
|
||||
Number(value); //converts value to a number
|
||||
|
@ -200,7 +200,7 @@ typeof var_ == "number"; // typeof returns a string with the name of the type
|
|||
|
||||
### Type Checking
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
isNaN(var); // converts var in number and then check if is NaN
|
||||
|
||||
Number("A") == NaN; //false ?!?
|
||||
|
@ -209,7 +209,7 @@ Number("A") == NaN; //false ?!?
|
|||
|
||||
### Dangerous & Stupid Implicit Type Casting
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
2 + 'text'; //"2text", implicit conversion and concatenation
|
||||
1 + "1"; //"11", implicit conversion and concatenation
|
||||
"1" + 1; //"11", implicit conversion and concatenation
|
||||
|
@ -305,7 +305,7 @@ Number("A") == NaN; //false ?!?
|
|||
|
||||
### IF-ELSE
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
if (condition) {
|
||||
//code here
|
||||
} else {
|
||||
|
@ -315,7 +315,7 @@ if (condition) {
|
|||
|
||||
### IF-ELSE Multi-Branch
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
if (condition) {
|
||||
//code here
|
||||
} else if (condition) {
|
||||
|
@ -331,7 +331,7 @@ if (condition) {
|
|||
|
||||
### Switch Statement
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
switch (expression) {
|
||||
case expression:
|
||||
//code here
|
||||
|
@ -347,7 +347,7 @@ switch (expression) {
|
|||
|
||||
### While Loop
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
while (condition) {
|
||||
//code here
|
||||
}
|
||||
|
@ -355,7 +355,7 @@ while (condition) {
|
|||
|
||||
### Do-While Loop
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
do {
|
||||
//code here
|
||||
} while (condition);
|
||||
|
@ -363,7 +363,7 @@ do {
|
|||
|
||||
### For Loop
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
// basic for
|
||||
for (begin; condition; step) { }
|
||||
|
||||
|
@ -384,7 +384,7 @@ iterable.forEach(() => { /* statements */ });
|
|||
`break;` exits the loop.
|
||||
`continue;` skip to next loop cycle.
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
label: for(begin; condition; step) {
|
||||
//code here
|
||||
}
|
||||
|
@ -394,7 +394,7 @@ break label; //breaks labelled loop and nested loops inside it
|
|||
|
||||
## Arrays
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let array = []; // empty array
|
||||
let array = ["text", 3.14, [1.41]]; // array declaration and initialization
|
||||
|
||||
|
@ -417,7 +417,7 @@ array.splice(start, deleteCount, [items_to_add]); // remove and RETURN items fr
|
|||
|
||||
### `filter()` & `map()`, `reduce()`
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let array = [ items ];
|
||||
|
||||
// execute an operation on each item, producing a new array
|
||||
|
@ -432,7 +432,7 @@ array.reduce((x, y) => ...);
|
|||
|
||||
## Spread Operator (...)
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// arrays
|
||||
let array1 = [ 1, 2, 3, 4, 5, 6 ];
|
||||
let array2 = [ 7, 8, 9, 10 ];
|
||||
|
@ -457,7 +457,7 @@ func(arg0, ...args);
|
|||
|
||||
## Dictionaries
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let dict = { FirstName: "Chris", "one": 1, 1: "some value" };
|
||||
|
||||
|
||||
|
@ -471,7 +471,7 @@ dict.FirstName = "Chris";
|
|||
|
||||
### Iterating Key-Value pairs
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
for(let key in dict) {
|
||||
let value = dict[key];
|
||||
|
||||
|
@ -485,7 +485,7 @@ Object.keys(dict).forEach(key => { });
|
|||
|
||||
### JSDOC documentation standard
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
/**
|
||||
* @param {type} parameter - description
|
||||
* @returns {type} parameter - description
|
||||
|
@ -494,7 +494,7 @@ Object.keys(dict).forEach(key => { });
|
|||
|
||||
### Function Declaration
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
// ...args will contain extra parameters (rest argument)
|
||||
function functionName(parameter=default-value, ...args) {
|
||||
//code here
|
||||
|
@ -504,7 +504,7 @@ function functionName(parameter=default-value, ...args) {
|
|||
|
||||
### Default Parameters (old versions)
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
function functionName(parameters) {
|
||||
if (parameter == undefined) {
|
||||
parameter = value;
|
||||
|
@ -517,7 +517,7 @@ function functionName(parameters) {
|
|||
|
||||
### Function Expressions
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
let functionName = function(parameters) {
|
||||
//code here
|
||||
return expression;
|
||||
|
@ -526,7 +526,7 @@ let functionName = function(parameters) {
|
|||
|
||||
### Arrow Functions
|
||||
|
||||
```javascript
|
||||
```javascript linenums="1"
|
||||
(input) => { /* statements */ }
|
||||
(input) => expression;
|
||||
input => expression; // parenthesis are optional
|
||||
|
@ -552,7 +552,7 @@ An object is a collection of related data and/or functionality.
|
|||
|
||||
**Note**: It's not possible to transform a variable in an object simply by using the object assignment.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let variable = value;
|
||||
|
||||
// object literal
|
||||
|
@ -596,7 +596,7 @@ Object.entries(obj); // list contents as key-value pairs
|
|||
JavaScript uses special functions called **constructor functions** to define and initialize objects and their features.
|
||||
Notice that it has all the features you'd expect in a function, although it doesn't return anything or explicitly create an object — it basically just defines properties and methods.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// constructor function definition
|
||||
function Class(params) {
|
||||
this.property = param;
|
||||
|
@ -622,7 +622,7 @@ An object's prototype object may also have a prototype object, which it inherits
|
|||
This is often referred to as a **prototype chain**, and explains why different objects have properties and methods defined on other objects available to them.
|
||||
If a method is implemented on an object (and not it's prototype) then only that object will heve that method and not all the ones that come from the same prototype.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// constructor function
|
||||
function Obj(param1, ...) {
|
||||
this.param1 = param1,
|
||||
|
@ -640,7 +640,7 @@ obj.method(); // call method from prototype
|
|||
|
||||
### Extending with prototypes
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// constructor function
|
||||
function DerivedObj(param1, param2, ...) {
|
||||
Obj.call(this, param1); // use prototype constructor
|
||||
|
@ -661,7 +661,7 @@ dobj.method(); // call method from prototype
|
|||
|
||||
### Classes (ES6+)
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
class Obj {
|
||||
constructor(param1, ...) {
|
||||
this.param1 = param1,
|
||||
|
@ -686,7 +686,7 @@ obj.func(); // call method
|
|||
|
||||
### Extending with Classes
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
class DerivedObj extends Obj {
|
||||
constructor(param1, param2, ...){
|
||||
super(param1); // use superclass constructor
|
||||
|
@ -704,7 +704,7 @@ dobj.newFunc();
|
|||
|
||||
### Object deconstruction
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let obj = {
|
||||
property: value,
|
||||
...
|
||||
|
@ -717,14 +717,14 @@ let { property: var1, var2 = default_value } = obj; // use default values if ob
|
|||
|
||||
### Array Deconstruction
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let array = [ 1, 2, 3, 4, 5, 6 ];
|
||||
let [first, , third, , seventh = "missing" ] = array; // extract specific values from array
|
||||
```
|
||||
|
||||
## Serialization
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let object = {
|
||||
// object attributes
|
||||
}
|
||||
|
@ -741,7 +741,7 @@ let object = JSON.parse(json); // deserialize to Object
|
|||
|
||||
Function runs *once* after an interval of time.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// param1, param2, ... are the arguments passed to the function (IE9+)
|
||||
let timerId = setTimeout(func [, milliseconds, param1, param2, ... ]); // wait milliseconds before executing the code (params are read at execution time)
|
||||
|
||||
|
@ -776,7 +776,7 @@ useTimeout();
|
|||
|
||||
### `let` vs `var` with `setTimeout`
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// let instantiates a new variable for each iteration
|
||||
for (let i = 0; i < 3; ++i) {
|
||||
setTimeout(function() {
|
||||
|
@ -795,7 +795,7 @@ for (var i = 0; i < 3; ++i) {
|
|||
|
||||
### Preserving the context
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let obj = {
|
||||
prop: value,
|
||||
|
||||
|
@ -824,7 +824,7 @@ let obj = {
|
|||
|
||||
Function runs regularly with a specified interval. JavaScript is **Single Threaded**.
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// param1, param2, ... are the arguments passed to the function (IE9+)
|
||||
let timerId = setInterval(func, milliseconds [, param1, param2, ... ]); // (params are read at execution time)
|
||||
|
||||
|
@ -852,7 +852,7 @@ There are generally 4 types of JavaScript date input formats:
|
|||
- Long Date: `"Mar 25 2015"` or `"25 Mar 2015"`
|
||||
- Full Date: `"Wednesday March 25 2015"`
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// constructors
|
||||
new Date();
|
||||
new Date(milliseconds);
|
||||
|
@ -892,7 +892,7 @@ let date = new Date(msec);
|
|||
|
||||
Comparison operators work also on dates
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
let date1 = new Date();
|
||||
let date2 = new Date("May 24, 2017 10:50:00");
|
||||
|
||||
|
@ -909,20 +909,20 @@ if(date1 > date2){
|
|||
|
||||
> **Note**: Firefox 68 and later define the origin of a page opened using a `file:///` URI as unique. Therefore, other resources in the same directory or its subdirectories no longer satisfy the CORS same-origin rule. This new behavior is enabled by default using the `privacy.file_unique_origin` preference.
|
||||
|
||||
```json
|
||||
```json linenums="1"
|
||||
"privacy.file_unique_origin": "false"
|
||||
```
|
||||
|
||||
In `page.html`
|
||||
|
||||
```html
|
||||
```html linenums="1"
|
||||
<script src="scripts/module.js"></script>
|
||||
<script src="scripts/script.js"></script>
|
||||
```
|
||||
|
||||
In `module.js`:
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
// exporting individual fractures
|
||||
export default function() {} // one per module
|
||||
export func = () => expression; // zero or more per module
|
||||
|
@ -942,7 +942,7 @@ export { func } from "other_script.js"
|
|||
|
||||
In `script.js`:
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
import default_func_alias, { func as alias } from "./module.js"; // import default and set alias
|
||||
import { default as default_func_alias, func as alias } from "./module.js"; // import default and set alias
|
||||
|
||||
|
@ -951,7 +951,7 @@ default_func_alias();
|
|||
alias();
|
||||
```
|
||||
|
||||
```js
|
||||
```js linenums="1"
|
||||
import * from "./module.js"; // import all
|
||||
|
||||
module.function(); // use imported content with fully qualified name
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue