Big O notation
Big O notation is used in computer science to describe the the performance or complexity of an algorithm. Big O specifically describes the the worst case scenario, and can be used to describe the execution time required or the space used.
References
- https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/
- https://justin.abrah.ms/computer-science/big-o-notation-explained.html
- https://www.interviewcake.com/article/javascript/big-o-notation-time-and-space-complexity
JavaScript Closures
Closures are function which ‘remember’ the environment in which they were created. A closure is a combination of a function and the lexical environment within which the function was declared. This environment consists of any local variables that were in-scope at the time closure was created.
The word lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.
function foo() {
var i = 10;
function print() {
console.log(i);
}
return print;
}
var printFunc = foo();
printFunc(); // 10
Common mistake of using closure in for loops
for (var x = 0; x < 10; x++) {
setTimeout(function() {
console.log(x);
}, 10);
}
// prints 10, 10 times
Solution, is to use a function factory
function myFunc(p) {
return function() {
console.log(p);
}
}
for (var x = 0; x < 10; x++) {
setTimeout(myFunc(x), 10);
}
// prints 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
References
let and const in ES6
ECMAScript 2015 or commonly called as ES6 specification of JavaScript introduced let
and const
let
let
is the new var
.
let
could be used to declare variables, just like var
.
Differences between let and var are
let
variables are block scoped.- Global
let
variables are not properties on the global object. - Loops of the form
for (let x...)
create a fresh binding for x in each iteration. - It’s an to try to use a
let
variable before its declaration is reached. - Redeclaring a variable with
let
is a SyntaxError.
const
Variables declared with const
are like let
except you can’t assign
to them, except at the point where they are declared.
The const
declaration creates a read-only reference to a value.
It does not mean the value it holds is immutable, just that the
variable identifier cannot be reassigned.
References
ES6 Arrow functions
Arrow functions are a more concise syntax for writing function expressions. They were introduced in ECMAScript 2015 (ES6). They are also called as lambda functions in other programming languages.
Advantages of Arrow functions
- More concise
- Arrow functions do not create its own this context. so
this
has it original meaning from the enclosing context.
Things to watch out for when using Arrow functions
- call, apply, bind do not change the value of this in Arrow functions.
- Arrow functions cannot be used as constructors
- Arrow functions cannot be used as generators.
- Arrow functions do not have the local variable
arguments
as do other functions.
References
Difference between ReactJS prop vs state
Props
Props are read only. Whether a component is defined as a function or a class, it must never modify its own props. All react component must act like pure functions with respect to their props.
function TitleComponent(props) {
return <h1>{props.title}</h1>;
}
State
State is similar to props, but its private and fully controlled by the component. State is local or encapsulated and its not accessible to any component other than the one that owns it. State is changeable. Do not modify state directly, Instead use setState() function. State updates may be asynchronous, to fix it, use second form of setState() that accepts a function rather than a object. React calls render method after state is changed to learn what should be on the screen.
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {name: "Hello World"};
}
render() {
return (
<form>
<input type="text"
value={this.state.name}
onChange={this.handleOnChange.bind(this)}/>
</form>
)
}
handleOnChange(e) {
this.setState({
name: e.target.value
})
}
}