Chapter 1 A quick Intro to Base objects
1.1 Launch a REPL
- For interactive usage
library(bubble)
node_repl()
1.2 Assignment
- Done with
=
var x = 12;
1.3 Scalar objects
1.3.1 Numbers
var y = 17;
x + y;
# 29
1.3.2 Character
var z = "plop";
# undefined
- All objects have methods that can be accessed with
obj.
z.toUpperCase();
# 'PLOP'
z.length;
# 4
- To know all the available methods
var prot = Object.getPrototypeOf(z);
Object.getOwnPropertyNames(prot);
# undefined
# [ 'length',
# 'constructor',
# 'anchor',
# 'big',
# 'blink',
# 'bold',
# 'charAt',
# 'charCodeAt',
# 'codePointAt',
# 'concat',
# 'endsWith',
# 'fontcolor',
# 'fontsize',
# 'fixed',
# 'includes',
# 'indexOf',
# 'italics',
# 'lastIndexOf',
# 'link',
# 'localeCompare',
# 'match',
# 'normalize',
# 'padEnd',
# 'padStart',
# 'repeat',
# 'replace',
# 'search',
# 'slice',
# 'small',
# 'split',
# 'strike',
# 'sub',
# 'substr',
# 'substring',
# 'sup',
# 'startsWith',
# 'toString',
# 'trim',
# 'trimLeft',
# 'trimRight',
# 'toLocaleLowerCase',
# 'toLocaleUpperCase',
# 'toLowerCase',
# 'toUpperCase',
# 'valueOf',
# 'trimStart',
# 'trimEnd' ]
1.3.3 Boolean
var a = true;
# undefined
if
are written as in Rconsole.log()
prints to the console (useful for debugging)
if (a) {
console.log("yeay");
}
# yeay
# undefined
1.3.4 Null & undefined
null
is an empty, non existing value that has to be defined
null
# null
undefined
is a declared variable that has no value
You’ll see undefined when doing assignment, it’s the return value of
=
var noval;
# undefined
noval;
# undefined
1.4 Array
They are R vectors
Can’t be named
Access by position
Note: INDEX STARTS AT 0
1.4.1 Create
- Can be created empty
var emptyarray = [];
This type of creation is called “Literals”
- Can be created with values
var x = ["pouet", "plop"];
- Can be created with the
new
function
var g = new Array;
# undefined
g
# []
var x = ["pouet", "plop"];
x[0] = "plop"
x
# [ 'plop', 'plop' ]
1.4.2 Select
- By position, (index start at 0)
x[0]
# 'plop'
- Filter
var poum = "plop";
x.filter(function(item){
return item === poum
});
# undefined
# undefined
# [ 'plop', 'plop' ]
Anonymous functions can be used here (see below for more info)
x.filter(item => item === poum);
# [ 'plop', 'plop' ]
1.4.3 Manipulate
- Iterate
with
forEach
x.forEach(function(item, index) {
console.log(index + " : " + item);
});
# 0 : plop
# 1 : plop
# undefined
with
map
x.map(z => z.toUpperCase());
# [ 'PLOP', 'PLOP' ]
Example, extracted from https://colinfay.me/aoc-2019-01/
var res = [105311, 117290, 97762, 124678, 132753, 114635];
var res = res.map(x => Math.floor(x / 3) - 2);
res
# undefined
# undefined
# [ 35101, 39094, 32585, 41557, 44249, 38209 ]
- Reduce
var add = (x, y) => x + y;
res.reduce(add)
# undefined
# 230795
- Finding the first element
var z = [1,2,3,4,5];
z.find(z => z > 3);
# undefined
# 4
- Test if every / some
x.every(z => z === "plop");
# true
x.some(z => z == "plop");
# true
Add / Suppress
- At the end
// Adds
x.push("bim");
x
# [ 'plop', 'plop', 'bim' ]
// Removes
x.pop();
x
# [ 'plop', 'plop' ]
- At start
// Adds
x.unshift("bim")
x
# [ 'bim', 'plop', 'plop' ]
// Removes
x.shift()
x
# [ 'plop', 'plop' ]
- By index
x.push("bim")
x.push("blam")
x
# [ 'plop', 'plop', 'bim', 'blam' ]
Takes 1 argument, starting at index 0
x.splice(0, 1)
x
# [ 'plop', 'bim', 'blam' ]
1.4.4 Transform
x.reverse();
# [ 'blam', 'bim', 'plop' ]
x.sort()
# [ 'bim', 'blam', 'plop' ]
var y = ["plaf", "plouf"]
x.concat(y)
# undefined
# [ 'bim', 'blam', 'plop', 'plaf', 'plouf' ]
x.join()
# 'bim,blam,plop'
var z = [1, 2, 3]
z.toString()
# undefined
# '1,2,3'
1.5 Objects
More or less like an R list
Constructed with
{}
var obj = {
a: 12,
b: 17,
c: ["plaf", "plouf"],
d: (x) => x.toUpperCase()
}
obj
# undefined
# { a: 12, b: 17, c: [ 'plaf', 'plouf' ], d: [Function: d] }
- Access with
.name
obj.a;
# 12
- Internal methods can be used on internal objects
obj.c.map(obj.d);
# [ 'PLAF', 'PLOUF' ]
1.6 Function
- Standard
function add(x, y){
return x + y
}
# undefined
add(11, 13)
# 24
- With the
new
constructor
var add2 = new Function('a', 'b', 'return a + b');
# undefined
add2(11, 13)
# 24
- As an anonymous (or arrow) function
var add3 = (a, b) => a + b
# undefined
add3(11, 13)
# 24
1.7 typeof
typeof a;
typeof z;
# 'boolean'
# 'object'