Some node code elaborated


const ftp = require("basic-ftp")
const client = new ftp.Client()

what is const in javascript

Search for: what is const in javascript

Here is Javascript ref from MDN: Mozilla developer network


const a = "hi"

//you then cannot say
a = "bye"

//throws an exception

try { 
  a = "bye"
}
catch (err)
{
  console.log("Sorry");
}

Similar to "let"

They are block scoped variables

An inner block, if repeats a name, will assume a different variable altogether

The let variable is available for use in inner blocks

Undeclared variables anywhere are global

Declared variables are executed before any code is executed

It is recommended to declare all variables

strict mode enforces it

Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.

It's important to point out that the hoisting will affect the variable declaration, but not its value's initialization. The value will be indeed assigned when the assignment statement is reached:

variables and function scope vs block scope

Search for: variables and function scope vs block scope

Here is an article on understanding contexts and scopes

Apparently something to do with if a function is invoked on an object.

it is visible in the entire function including nested functions

Understand new operator in javascript


//Just define
//any functoin
function foo()
{
}

//use the new operator
var f = new foo()

//creates memory
//calls it this
//gives an opportunity for foo to
//initialize this
//and assigns that memory to "f"

//exmaple of using this
function foo1()
{
  this.a =" b";
  //return of this is implied
}

Javascript difference between a function call and a new

Search for: Javascript difference between a function call and a new


var Obj =
{
  a: "a",
  b: "b"
}

What if I forget to include new?


calling new on a nonstructor function
Forgetting new on a construtor function

function Car() {}
car1 = new Car();
car2 = new Car();
 
console.log(car1.color);    // undefined
 
Car.prototype.color = "original color";
console.log(car1.color);    // original color
 
car1.color = 'black';
console.log(car1.color);   // black

console.log(car1.__proto__.color) //original color
console.log(car2.__proto__.color) //original color
console.log(car1.color)  // black
console.log(car2.color) // original color

var, const, let

Search for: var, const, let

Var outside of any function are global

Var inside a function does not recognize blocks. it is function scoped

a variable inside a for loop or a block, will not contaminate a similarly named variable outside the block.

Const and let are used in such cases


Global scope
function scope
block scope