Javascript Notes - Aug 2019
Go for the cloud, Javascript for the GUI. ReactJs for the framework. Note this stack is aiming to use the language most suitable for the target - ie the native language for that use-case. With as few extra bits or pieces as possible.
So, I’ve learnt Go and I have been using js and Reactjs for a month now, time to make some notes on Javascript.
Values
string, number, boolean, object, function
Objects
Object, Date, Array, String, Number, Boolean
No values
null, undefined
Operators on types
typeof “Anomanda” // returns string
Dates
https://www.w3schools.com/js/js_date_methods.asp
Looping
let myArray= ["one", "two", "three", "four"];
for(let i = 0; i < myArray.length; i++){
// can use break to exit
}
myArray.forEach(function(element, index) {
// cannot use break
});
var person = {fname:"John", lname:"Doe", age:25};
var x;
for (x in person) {
// loop through an object
}
for (x of myArray) {
// iterable such as Arrays, Strings, Maps, NodeLists, etc
}
while (i < 10) {
i++;
}
do { // do it at least once
i++;
}
while (i < 10);
Object cloning with the spread syntax:
let objClone = {...obj};
Or to clone it, but alter just one field, eg adding 1 to the count field.
let objClone = {...obj, count: obj.count+1 };
Rest syntax for multiple arguments as an arrays
function sum(...theArgs) {
return theArgs.reduce((prev, curr) => {
return prev+curr;
})
}
console.log(sum(1,2,3));
ReactJs
https://itnext.io/react-hooks-context-without-redux-401235df2a68
https://reactjs.org/docs/context.html
https://reactjs.org/docs/hooks-reference.html#usecontext
This is better than any other Hooks guide I’ve seen. It makes it look like the old swing model view controller paradigm.
The reducer is the controller, and the context is the model and the hooks function is the view.
testing
https://jestjs.io/docs/en/getting-started.html
npm install --save-dev jest
npm run test
also, because I have a conflict I have a .env file with
SKIP_PREFLIGHT_CHECK=true
Don’t forget GoLand will also run your Jest tests.