Some node code elaborated

satya - 4/24/2019, 9:57:25 PM

Lets look at this


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

satya - 4/24/2019, 9:57:38 PM

what is const in javascript

what is const in javascript

Search for: what is const in javascript

satya - 4/24/2019, 10:01:28 PM

Here is Javascript ref from MDN: Mozilla developer network

Here is Javascript ref from MDN: Mozilla developer network

satya - 4/24/2019, 10:03:38 PM

Const means you cannot reassign that variable to something else


const a = "hi"

//you then cannot say
a = "bye"

//throws an exception

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

satya - 4/24/2019, 10:13:11 PM

Const is block scoped


Similar to "let"

satya - 4/24/2019, 10:14:39 PM

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

satya - 4/24/2019, 10:23:30 PM

On variables

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:

satya - 4/24/2019, 10:27:44 PM

variables and function scope vs block scope

variables and function scope vs block scope

Search for: variables and function scope vs block scope

satya - 4/24/2019, 10:37:21 PM

Here is an article on understanding contexts and scopes

Here is an article on understanding contexts and scopes

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

satya - 4/24/2019, 11:08:39 PM

Bottom line on a var inside a function

it is visible in the entire function including nested functions

satya - 5/11/2019, 7:22:26 PM

Understand new operator in javascript

Understand new operator in javascript

satya - 5/11/2019, 7:32:47 PM

So what happens here


//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
}

satya - 5/11/2019, 7:34:31 PM

Javascript difference between a function call and a new

Javascript difference between a function call and a new

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

satya - 5/11/2019, 7:52:08 PM

Objects are just key/value pairs


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

satya - 5/11/2019, 7:53:28 PM

What if I forget to include new?

What if I forget to include new?

satya - 5/11/2019, 7:57:39 PM

Two possibilities


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

satya - 5/11/2019, 7:58:07 PM

Introducing prototype


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

satya - 5/11/2019, 7:58:29 PM

var, const, let

var, const, let

Search for: var, const, let

satya - 5/11/2019, 8:05:34 PM

Var outside of any function are global

Var outside of any function are global

satya - 5/11/2019, 8:06:08 PM

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

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

satya - 5/11/2019, 8:07:28 PM

This means

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

satya - 5/11/2019, 8:07:38 PM

Const and let are used in such cases

Const and let are used in such cases

satya - 5/11/2019, 8:09:51 PM

So three scopes


Global scope
function scope
block scope