ES6 Flashcards
Master the language
Arrow functions props
- Cannot be used as constructors;
- They have no “this”;
- They have no “super”;
- They have no new.target;
- They have no prototype;
- They have no “arguments” object;
- They have no duplicate named parameters.
What do arrow functions of 1 parameter return?
The expression to the right
In what do you have to wrap an arrow function that returns an object literal?
In ( ); Example: let myResult = id => ({id: id, name: "RSE});
Can you change “this” by using bind(), call() or apply() in arrow functions?
No
Can you access in arrow function the “arguments” from a function that contains it?
Yes. Ex: function containingFunction(){ return () => argument[0]; }
var myFunction = containingFunction(5); console.log(myFunction()); // 5
Can you use bind(), call() and apply() on arrow functions?
yes
What is the difference between the new string methods includes() and startsWith() on one side, and endsWidth() on the other side? Clue: something related to 2nd parameter
endsWidth() starts the match from the length of the string minus the 2nd argument, whereas startsWith() and includes() start the match from that index given by the 2nd arg
Having a function: function makeRequest(a,b=3,c){ return a+b+c; }
what is the function returning if I do makeRequest(2,null,5)?
a. null;
b. 10;
c. 7;
d. 192
c) because if I use null for a default parameter, null is considered valid and that value will NOT be used.
How is the behaviour of “arguments” different in ES5 vs ES6?
In ES5 non-strict mode, “arguments” will “follow” a function named arguments;
In ES5 strict mode, “arguments” does not reflect the changes of named parameters;
In ES6, “arguments” is detached from named arguments, irrespective of strict or non-strict node.
If I have a function:
function mixArgs(first,second=”b”){ console.log(arguments)}
- What is the value of arguments[1] if I call the function as in mixArgs(100)?
- Also what is the function call f=mixArgs(100) going to print?
- undefined, because arguments[1] = undefined
2. Arguments [100, calee(…), Symbol(Symbol.iterator):f]
Why is possible and correct to write:
function getValue(value){ return value+5};
function add(first, second = getValue(first)){ return first + second;}
Because in ES6 it is allowed to use a previous parameter as a default for a second parameter.
(not allowed the other way around though)
In loops, in order to avoid creating closures, what is the recommended concept to use instead of plain functions?
IIFEs;
What is Javascript doing when it looks through an upcoming block and finds a variable declaration?
What is the difference behaviour between “var” compared to “let” and “const” declarations in this case?
The declaration with “var” is hoisted at the beginning of the current scope;
The “const” and “let” declarations are placed in the TDZ, where they will stay until that execution flow passes the declaration point.
“let” and “const” are NOT hoisted!!
Which ones of the two typeof instructions below will return “undefined” and which one will throw an error?
console.log(typeof(a)); let f3 = function() { console.log(typeof(a)); let a = 3; }
The 2nd typeof will throw an error, because a is in the TDZ at the moment of the call of that typeof, whereas the first typeof will just return “undefined”, because the variable a at that scope is not in the TDZ (is no blocked by a “let” or a “const” declaration at that scope level)
When is the parameter initialization happening for a function?
a. When the function is declared;
b. When the function is called;
When the function is called
Function parameters and function body have:
a. the same scope or a different scope;
b. the same TDZ or a different TDZ.
Both are different»_space;> a function’s default params cannot access any variables declared inside the function body
What is the “pick()” function in the “Underscore.js” library doing?
Returns a copy of a given object with some specific subset of the original properties.
What is the function presented below going to return if called as: let f = function(x,...y){ console.log(arguments.length)} let a = 100; let b = [200, 300]; f(a,b);
3
What is the console.log() presented below going to print?
let f = function(x,...y){ x+y[0]} let a = 100; let b = [200, 300]; console.log(f.length);
1, and not 2 or 3, because rest parameters DO NOT affect the function’s length property
What are the rest parameters restrictions?
- There can only be one rest parameter;
- The rest parameter has to be the last of the function parameters;
- The rest parameters cannot be used in an object literal setter, like:
let object = { // Syntax error set name(...value){ // do something } };
How are the rest parameters influencing the “arguments” object for a function?
There is no influence, “arguments” will reflect 3 if f(a,…b) and a=100,b=200,c=300.
What is the spread operator used usually to replace?
The apply() method as it is far easier to do
Math.max(…values, 0)) where
values=[100,200];
What is the restriction on the object literal setters?
They are restricted to a single argument
What are the differences between spread parameters and rest arguments?
1. Spread parameters are used when you call the function: var abc = ['a', 'b', 'c']; var def = ['d', 'e', 'f']; var alpha = [ ...abc, ...def];
Rest arguments are used when you define a function: function sum(first, ...others){.........};
- Rest argument has to be only one, whereas the spread parameters can be as many as you like;
- The rest argument has to come on the last position, whereas the spread arguments can have any position.
What is the console.log() going to print?
var doSomething = function doSomethingElse(){ } console.log(doSomething.name);
doSomethingElse
What is the console.log() instructions below going to print?
var person = { get firstName () { return "Nicholas" }, sayName: function(){ console.log(this.name); } } 1. console.log(person.sayName.name); 2. console.log(person.firstName.name);
- “sayName”
2. “get firstName”
Why is it not possible to use the “name” property of a function to get access to it?
because the “name” property is only meant to be informative (in the debugging process, for ex.)
What is the console.log() instruction going to be print?
var doSomething = function(){ };
console.log(doSomething.bind().name);
“bound doSomething” as functions created with “bind” will prefix their name with “bound”.
What is the console.log() instruction going to be print?
console.log((new Function()).name);
“anonymous” because functions created using the Function constructor use the name “anonymous”
What is the role of the new.target metaproperty in ES6?
Is to solve the problem of naming functions created with "new". Inside a function declaration, new.target will filled with something (the target of the "new" operator), so I can use it to detect if a function has been called with "new" or not: function Person(name){ if (typeof new.target !== "undefined"){ this.name = name; } else { throw new Error("You must use new with person"); } }
Can you call an arrow function as a constructor?
No, is not allowed in ES6
Having the following code:
"use strict"; if(true) { console.log(typeof doSomething); function doSomething(){ } doSomething(); } console.log(typeof doSomething);
What are the two console.log() instructions going to print?
- “function” because the function declaration within the if block will be hoisted to the top of the if block
- “undefined” because doSomething() is inexistent outside the block within it has been defined.
Having the following code:
“use strict”;
if (true) { console.log(typeof doSomething); let doSomething = function() { }; doSomething(); } console.log(typeof doSomething);
What are the two console.log() instructions going to print?
- throws an error because “let” expressions do not hoist in strict mode;
- “undefined”;
What is the difference between block-level functions’ behaviour in “strict” mode compared to “non-strict” mode?
In “strict” mode, the block-level functions are being hoisted at the top of the containing block, whereas in “non-strict” mode they are being hoisted all the way to the containing function or the global environment.
Having the following code:
if(true) { console.log(typeof doSomething); function doSomething(){ } doSomething(); } console.log(typeof doSomething);
What are the two console.log() instructions going to print?
- “function” because the function declaration within the if block in non-strict mode will be hoisted to the global scope or the containing function (in this case is global scope)
- “function” because doSomething() is hoisted in this case to the global scope
How is the “this” value going to be defined in an arrow function?
By the closest non-arrow function that surrounds my arrow function, or by the global scope.
How do you create an IIFE out of an arrow function?
By surrounding it in ( )
What is the difference between concise methods and non-concise methods in respect with “super”?
The concise methods can access “super”, whereas the non-concise methods cannot;
What is the result of this operation? NaN === NaN
false
What is Object.assign() used for? How does it work? What does it return?
For mixins. It accepts a receiver and lots of suppliers, and then returns the receiver.
What is lost when a supplier is using Object.assign() to create a mixin?
It lossed the accessor properties.
What is the following code going to do and why? var obj = { a:1, 0:1, c:1, 2:1, b:1, 1:1 }
obj. d = 1;
console. log(Object.getOwnPropertyNames(obj).join(“,”));
“0,1,2,a,c,b,d” because it groups all numeric keys will be group and sorted, however the string keys will appear in the order they were added.
Also the keys in the object literal come first, followed by the keys added afterwards.
What is the key enumeration method for for-in loops, for Object.keys() and for JSON.stringify() ?
All have “unspecified” enumeration order
How is an object’s prototype specified in JavaScript?
In two ways: when the object is created via either a constructor or via Object.create() method.
What is the difference between ES5 and ES6 in terms of prototype changing?
In ES5 is not allowed, however in ES6 the new method Object.setPrototypeOf(obj, prototype) allows it.
Caveat: it is a very slow operation, better to use Object.create()
Do const, let and var need initializers?
"const" always requires an initializer; "let" and "var" need one only when you're using destructuring: e.g. let node = { type: "identifier", name: "foo" }
What happens if you try to read null or undefined in JavaScript?
It throws a runtime error.
What is the “value” local variable going to be in the code below:
let node = { type: "id", name:"foo" }; let {type, name, value} = node;
“value” will be undefined.
Having two variables: let a = 1, b =2; How do swap them in a single instructions by using destructuring?
[a,b] = [b,a]
How do you clone arrays in ES5 compared to ES6?
In ES5: var colors = ["red","blue","green"]; var clonedColors = colors.concat();
In ES6:
let colors = [“red”,”blue”,”green”];
let [ …clonedColors] = colors; //using the rest parameters
In ES6, how can you pull out values of a JSON structure?
By using mixed object and array destructuring: let node = { type: "id", name: "foo", loc: { start: { line:1, col:1 }, end: { line:1, col:4 } } range: [0,3] };
let { loc: {start}, range: [startIndex] } = node; console.log(start.line); // 1 console.log(start.col); // 1 console.log(startIndex); //0
How do you create a Symbol in ES6?
let user_id = Symbol.for(“user_id”);
What do you get below?
var uid = Symbol.for("uid"); let desc = String(uid); console.log(desc+uid);
Uncaught Error: Cannot convert a Symbol value to a string
What do you have to use to overwrite a nonwritable property of an object?
Object.defineProperty()
e.g
let car = { }; Object.defineProperty(car,"speed",{ configurable:true, writable:false, value:1}) car.speed; // 1 car.speed = 4; car.speed; // 1 (unmodified)
Object.defineProperty(car,”speed”,{
configurable: true, writable: false, value: 100}) car. speed; // 100 (modified)
What is the following code doing?
ArraySymbol.hasInstance
is checking if “obj” is an instance of Array.
Symbol.hasInstance is a property of every function that can be used to check if an object is an instance of that function
What is the console.log( ) below actually display?
function SpecialNumber() { }
Object.defineProperty(SpecialNumber, Symbol.hasInstance, {
value: { function(v) {
return (v instanceof Number) && (v>=1 && v<=100);
});
var two=new Number(2); console.log(two instanceof SpecialNumber);
true, as I managed to change the instanceof method of SpecialNumber class
Having two variables: let colors1 = ["red", "green"], colors2=colors1.concat(["blue", "black"], "brown");
What is the console.log(colors2); going to show and why?
It is going to show [“red”, “green”, “blue”, “black”, “brown”] because the internal array is being split automatically by JavaScript.
In fact, JavaScript spec says that arrays are automatically split in their components, also that all other types are not.
What is the JavaScript realm? Give an example
It is an execution environment for JavaScript.
For example, an iframe and its containing page are two different realms. Each has its own global scope with its own copy of global objects.
When passed to a different realm, Arrays create problems because “instanceof Array” call return false.
What is Symbol.toStringTag used for?
Is to allow the programmer to define what the objects return when call Object.prototype.toString.call(obj )
In which mode is not allowed to use “with”?
strict mode
What is the classes and modules default mode?
strict mode
How can you convert the following set:
let set = new Set([1,2,3,3,3,5,6]) into an array?
let a = […set]; (use the set operator)
How do you eliminate duplicates from an Array easily in ES6?
By transforming the array into a set (new Set(my_array)) then converting back to an array: uniqueArray = [...mySet]
What happens if I add a key to a (non-weak) set then I erase the key?
The set still exists even if I put the key = null, because even if I deleted the external variable key, there is still an internal set reference to that object;
Can a weak set store objects and primitive values?
Yes for objects, no for primitive values
In which ones of the two below is not possible to store primitive objects?
Sets and Weak sets
In weak sets.
Having a function: let f = function( ) { }; and two sets: let set = new Set(); let weak_set = new WeakSet();
then in which is not possible to store f, like in:
set.add(f);
weak_set.add(f);
You can store f in both, as f is a function, hence not a primitive value; had it been a primitive value, it would’ve been impossible to store it in the weakset.
Which of the two types of sets cannot be used in (for…of) loops?
Weak sets, as they are not iterable
What is the operator … called in the code below:
let set = new Set([1,2,3,3,3,4]); array = [ ...set];
It is the spread operator
If we have a weak set: let set = new WeakSet( ), key = { };
What happens if we add a key as in:
set.add(key);
and then we delete the key? What is the key deletion instruction?
- the key in the set will go, as there is no other key to reference it;
- set.delete(key);
What are JavaScript objects always coercing their properties values into?
Into strings
What can I use as keys when I build a map in ES6?
You can use objects, numbers, functions
When storing data into a map, assuming the data is in a key/value format, what do you have to do to ensure the data is stored correctly into the map?
You have to store the pairs (store,key) in individual arrays:
e.g.
let m = new Map( [ [ “name”, “Nicholas”], [“age”, 25] ] );
What is the difference in terms of key-value pairs order between maps and objects when calling forEach()?
For maps the key-value pairs will be enumerated in the order of creation, whereas for objects the keys will be enumerated in the order of numeric indexes then in the order of creation for non-numeric keys
What is the condition that the keys of a weakmap have to fulfill?
They have to be non-null objects
Which one is ordered, which one is unordered:
WeakMap, Maps, Sets, WeakSets
WeakMaps are unordered;
Maps are ordered;
Sets are ordered;
WeakSets are unordered
What is missing below: let w = Weakmap();
The correct form is: let w = new Weakmap();
What is wrong below:
let w = WeakMap(); let w = new WeakMap();
- Uncaught TypeError: Constructor WeakMap requires ‘new’»_space;> so add “new” to the first statement
- Uncaught SyntaxError: ‘w’ has already been declared
If we have a WeakMap and we add a {key/value} pair, what happens if we delete the key?
What if ,instead of the above, we add an element to the same map: myMap.set( {myKey} ) ==> What happens if we then delete the key?
a. The {key, value} will NOT disappear, as only the keys are weakly stored in weak maps;
b. The {key} will disappear;
How do you make object properties public or private in ES6?
All object properties are public in ES6; whereas in ES5 you have to use IIFEs to create private properties, in ES6 you can use IIFEs and inside them you can use WeakMaps to store private info; the advantage is that doing this way, compared to ES5 way, when the object created by the IIFE constructor disappears, the private info disappears;
What is recommended to use instead of nested loops in ES6?
You can use iterators
What is an iterator?
Is an object which:
- have a next( ) method;
- the next( ) method returns a result object;
- the result object has two properties:
a. value ==> that is the next value;
b. done ==> a Boolean, true when no more values
What is a generator?
It is a function that returns an iterator
Having the following code:
function *createIterator(items){ items.forEach(function(item)){ yield item+1; }); }
you will get an error; what is the reason?
“yield” keyword can only be used inside iterators; technically, in this example “yield” is indeed inside the iterator, however is inside a function within the iterator.
The error will be “unexpected identifier”
What is wrong in the following code?
function *createIterator(x) { (x) => { yield x+1; } }
Is not possible to create iterators with arrow functions. You will get “SyntaxError: Unexpected identifier”
What is an iterable?
It is an iterator with a Symbol.iterator property
What is Symbol.iterator?
Is a property which specifies a function that returns an iterator for a given object.
Arrays, stes, maps and strings are iterables in ES6 and they have a default iterator specified.
What is for-of loop used for?
a for-off loop calls next( ) on an iterable each time the loop executes and stores the value from the result object in a variable:
let value = [1,2,3];
for (let num of values){
console.log(num)
}
All iterators created by generators are also iterables. Why?
Because generators assign the Symbol.iterator by default.
What is the code below doing?
return typeof object[Symbol.iterator] === “function”
Checking whether an object is iterable
How can you access the default iterator of an object?
You can use Symbol.iterator as per below:
let values = [1,2,3];
let iterator = valuesSymbol.iterator;
Are the developer-defined objects iterable by default?
No.
How do you make an object iterable?
By creating a Symbol.iterator property containing a generator:
let collection = { items: [ ], *[Symbol.iterator] ( ) { for (let item of this.items) { yield item; } } }; collection.items.push( 1 ); collection.items.push( 2 ); collection.items.push (3);
for (let x of collection ){
console.log(x);
}
What are the default iterators for Weak Maps and Weak Sets?
They don’t have built-in iterators;
What are the default iterators for arrays, maps, and sets?
arrays and maps is values( ), for sets is entries( )
Can you use the spread operators several times in an array?
Yes:
let smallNumber = [1, 2, 3];
let bigNumber = [100, 200, 300];
let allNumbers = [0, …smallNumbers, …bigNumbers];
Why are the iterator return values useful?
For delegating operators. The spread operator and the for-of ignore the return value.
Can generators be combined?
Yes: function *createNumberIterator ( ) { yield 1; yield 2; }
function *createColorIterator ( ) { yield "red"; yield "green"; }
function *createCombinedIterator( ) { yield *createNumberIterator( ); yield *createColorIterator( ); yield true; }
var iterator = createCombinedIterator( );
What is the yield instruction behaviour?
It stops execution and waits for the next( ) method to be called before starting again
How do you pass values into an iterator?
By passing values into the next( ) method
What is yield role in an generator?
It stops the generator execution and waits for the next() method to be called before starting again
How can you access the default iterator for an object?
You have to use the Symbol.iterator, like below:
let values = [1,2,3];
let iterator = valuesSymbol.iterator;
console.log(iterator.next)); // “{value:1, done:false}”
console.log(iterator.next)); // “{value:2, done:false}”
console.log(iterator.next)); // “{value:3, done:false}”
console.log(iterator.next)); // “{value:undefined, done:true}”
How do you make a developer-defined object iterable?
By creating a Symbol.iterator property that contains a generator: let collection = { items: [], *[Symbol.iterator] ( ) { for (let item of this.items) { yield item; } } };
collection. items.push(1);
collection. items.push(2);
collection. items.push(3);
for (let x of collection) {
console.log(x);
}
How many types of collection objects are in JavaScript?
three: arrays, maps and sets
For the three types of collection objects in JavaScript, enumerate the built-in iterators
- entries()»_space;> return iterator whose values are key/value pairs;
- values()»_space;> return iterator whose values are the values of the collection;
- keys()»_space;> return an iterator whose values are the keys contained in the collection;
For the three types of collection objects in JavaScript, which s the default iterator?
a. the values() is the default for arrays and sets
b. the entries() is the default for maps
What are the built-in iterators for weak sets and weak maps?
They DON’T have built-in iterators
How do you transform a Set into an Array?
by using the spread operator (…)»_space;> for ex:
let set = new Set([1,2,3,3,3,4,5]),
array = […set];
console.log(array);
What are the two ways that an iterator can pass values out?
a. by using the next( ) method;
b. by using the yield in a generator;
How can you pass values into an iterator?
By using the next( ) method also; when you pass an argument to the next( ) method, that argument becomes the value of the yield statement inside the generator;
How can you pass error conditions into iterators?
By building iterators that implement throw( ) method that instructs the iterator to throw an error when it resumes; you can also pass an error object into the throw( ) >>> ex: function *createIterator( ) { let first = yield 1; let second = yield first + 2; yield second + 3; }
let iterator = createIterator();
console. log(iterator.next( )); // {value:1,done:false}
console. log(iterator.next(4)); // {value:6,done:false}
console. log(iterator.throw(new Error(“boom”))); // error thrown from the generator
How do you catch errors inside the generators?
By using the usual try/catch inside the generators
What is the second call of console.log( ) in the code below going to produce?
function *createIterator( ) { yield 1; return; yield 2; yield 3; }
let iterator = createIterator( ) ;
console. log(iterator.next( ));
console. log(iterator.next( ));
{value:undefined, done:true}
Can iterators be combined?
Yes, by using generators delegation; for ex: function *createNumberIterator( ) { yield 1; yield 2; } function *createColorIterator( ) { yield "red"; yield "green"; } function *createCombinedIterator( ) { yield *createNumberIterator( ); yield *createColorIterator( ); yield true; }
let iterator = createCombinedIterator( ) ;
console.log(iterator.next( ));
…………………………….
console.log(iterator.next());
will print: {value:1,done:false} {value:2,done:false} {value:"red",done:false} {value:"green",done:false} {value:true, done:false} {value: undefined, done: true}
Give an example of a generator functions created with an arrow function.
It’s NOT possible
Can you use generators wherever you use functions?
Yes, because generators are actually functions, you can use them both the same way
Can you modify a class prototype?
No, as they are read-only
Are the classes hoisted?
Not, unlike function declarations, classes are not hoisted, they are like “let” declaration, that means they exist in the TDZ until the execution point reaches them
How can you opt out of the default strict mode in a class?
It’s not possible
What is the difference between classes and custom types in terms of method enumerability?
In classes, all the methods are nonenumerable, whereas in custom types you have to use Object.defineProperty( ) to make a method non-enumerable
What is the effect of the code below:
let person = new class { constructor(name){ this.name = name; } sayName(){ console.log(this.name); } }("Nicholas");
It actually creates a singleton
How do you create a default class iterator?
By using the syntagm *[Symbol.iterator] inside the class.
For example:
class Collection { constructor ( ) { this.items = [ ]; } *[Symbol.iterator]( ) { // this expression uses a // calculated expression that will be the //generator method, also uses delegation // to the value() iterator of the items; yield *this.items.values( ); } } var collection = new Collection( ); collection.items.push(1); collection.items.push(2); for (let x of collection) { // here is how I use the iterator) console.log(x); }
Can you access static members of a class from its instances?
No, you must access static members from its class only
What are the rules of using super( )?
- You can only use super( ) in a derived class constructor; if you use it in a non-derived class (one that doesn’t use “extends”, or in a function, will throw);
- You must call super( ) before accessing “this” in the constructor;»_space;> super( ) is responsible to initialize “this”;
- the only way to avoid calling super( ) is to return an object from the class constructor;
Are static members of a class inherited or not?
Yes
Can you dynamically use an expression to calculate the class that I want to extend?
Yes. For ex: function Rectangle( ) { ... } function getBase( ) { return Rectangle; } class Square extends getBase( ) { ..... }
What is the link between using “extends” and calling super( )?
They come in conjunction (in pairs): if you use “extends”, you must call super( )
What is the difference between ES5 classical inheritance and the ES6 class-based inheritance?
In the ES5 classical inheritance, the value of "this" is created first by the derived type, then the base type of the constructor is created; In the ES6 class-based inheritance, the value of "this" is first created by the base class and then modified by the derived class;
What are the three pillars of metaprogramming in ES6 and how are they used?
- Symbols are all about Reflection within implementation - you sprinkle them on your existing classes and objects to change the behaviour.
- Reflect is all about Reflection through introspection - used to discover very low level information about your code.
- Proxy is all about Reflection through intercession - wrapping objects and intercepting their behaviours through traps.
If you have a new class inheriting from a built-in class (like Array, for ex), and if I use a method on this new class, what type of object will it return - will it be the built-in class or the new class?
It will be the new class, because the way the Symbol.species behave;
Is it possible to change the species of a class?
No, you cannot change the Symbol.species.
What fo you use Symbol.species for?
It's a symbol used to define a static accessor property that returns a function. That function is a constructor to use whenever an instance of the class must be created inside an instance method (instead of using the constructor). In simple terms, is a method that returns "this" (aka the constructor function) In practice, you can use it to control the type of a derived class. For example, if you derive a built-in class and you check the type of the result of any method of the built-in class applied on the derived class (by using instanceOf( ) ), you will get the derived class, whereas if you use the Symbol.species in the derived class as a getter, you will get the built-in class type as a result.
Which built-ins have a Symbol.species defined?
Array; ArrayBuffer; Map; Promise; RegExp; Set; Typed Arrays
How do you get reference to an Array Symbol.species?
let mumu = [ ]; mumu.constructor[Symbol.species];
Where is the new.target property used?
Is always used inside class constructors
What is an abstract base class?
It’s a class that cannot be instantiated directly
How can you use new.target to build an abstract base class?
// abstract base class class Shape { constructor( ) { if (new.target === Shape) { throw new Error("This class cannot be instantiated directly."); } } }
What is the function below doing?
function myFunction(arrayLike) { return Array.prototype.slice.call(arrayLike); }
Is used in ES5 to transform an array-like object into an array
What is the way to convert an array-like object into an array in ES6?
By using the Array.from( ). For ex: function doSomething( ){ var args = Array.from(arguments); // us args; }
What is an array buffer in JavaScript ES6?
Is a memory location that can contain a specified number of bytes;
How do you create an array buffer in ES6?
let buffer = new ArrayBuffer(10); // allocates 10 bytes
How do you check the length of a array buffer?
using byteLength();
e.g. console.log(buffer.byteLength);
How to you change the size of an ArrayBuffer?
You cannot change the size of an ArrayBuffer.
What is a DataView type in ES6?
It is a “window” that allows you to manipulate the memory pointed by an ArrayBuffer
How do you define a DataView?
By using the DataView constructor. For ex.: let buffer = new ArrayBuffer(10); let view = new DataView(buffer);
How do you write into a DataView?
By using the DataView setInt (8,UInt), setFloat32, setFloat64, etc.
let buffer = new ArrayBuffer(10); let view = new DataView(buffer); view.setInt8(0,4) ==> writes a value of 4 starting with position 0;