Javascript journal

satya - 5/23/2019, 10:13:31 AM

How do I compare two javascript object references?

How do I compare two javascript object references?

Search for: How do I compare two javascript object references?

satya - 5/23/2019, 10:16:50 AM

Comparison operators in JS

Comparison operators in JS

satya - 5/23/2019, 10:17:28 AM

quick summary


console.log(1 == 1);
// expected output: true

console.log("1" == 1);
// expected output: true

console.log(1 === 1);
// expected output: true

console.log("1" === 1);
// expected output: false

satya - 5/23/2019, 10:17:59 AM

The 2 equals will coerce types

The 2 equals will coerce types

satya - 5/23/2019, 10:18:55 AM

The 3 equals

Both have to be of the same type

Contents must match

satya - 5/23/2019, 10:23:13 AM

Important thing is native types in JS are NOT objects

Important thing is native types in JS are NOT objects

satya - 5/23/2019, 10:25:14 AM

If what is compared are objects then only their REFERENCES in memory matter

If what is compared are objects then only their REFERENCES in memory matter

satya - 5/23/2019, 10:25:28 AM

How can I print an object reference in Javascript?

How can I print an object reference in Javascript?

Search for: How can I print an object reference in Javascript?

satya - 5/23/2019, 10:26:55 AM

Primitive types


Boolean
null
undefined
String
Number

satya - 5/23/2019, 10:27:08 AM

Objects


Array
Function
Object

satya - 5/23/2019, 10:35:46 AM

Here is that question on SOF

Here is that question on SOF

satya - 5/23/2019, 10:36:58 AM

Summary

There doesn't seem to be a mechanism to print the internal memory reference as you could have done in Java, say.

So you have to use equality operators, and you must know before hand that both sides are objects for sure!!!

satya - 5/23/2019, 10:42:33 AM

Thank God the single equality is still what it means: assignment.

Thank God the single equality is still what it means: assignment.

satya - 5/25/2019, 11:03:57 AM

Summary of comparisons

1. = means assignment

2. == means coerce the types (if they are primitive, including strings). Arrays are objects I believe and not primitive

3. === means do not coerce the types and declare they are not equal

4. when what is compared are objects then are equal only if their internal reference match

5. There is NO way you can print an object reference to see, like you may in Java

satya - 5/25/2019, 11:06:38 AM

How to read environment variables in Javascript

How to read environment variables in Javascript

Search for: How to read environment variables in Javascript

satya - 5/25/2019, 11:14:22 AM

That is talked about here in SOF

That is talked about here in SOF

satya - 5/25/2019, 11:14:38 AM

Example from there


var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; // '42348901293989849243'

satya - 5/25/2019, 1:45:06 PM

You can also do this for dynamic keys


this.ip = process.env[ipKey];
this.user = process.env[userKey];
this.password = process.env[passKey]

Notice the alternate syntax to access javascript arrays

satya - 5/25/2019, 1:45:21 PM

The toString function for javascript objects

The toString function for javascript objects

Search for: The toString function for javascript objects

satya - 5/25/2019, 1:50:50 PM

when conversion to string is needed this function is called on the object

when conversion to string is needed this function is called on the object

satya - 5/25/2019, 1:51:04 PM

Quick code sample


function createHostSpecFromEnv(name)
{
    this.name=name;

    let ipKey = "host_" + name + "_ip";
    let userKey = "host_" + name + "_user";
    let passKey = "host_" + name + "_password";

    this.ip = process.env[ipKey];
    this.user = process.env[userKey];
    this.password = process.env[passKey]

    //You can do this
    this.toString = function () {
        return this.ip + ";" + this.user + ";" + this.password;
    }

    //Or this arrow function convention
    this.toString1 =  () => {
        return this.ip + ";" + this.user + ";" + this.password;
    }
}

//Don't forget the new. You will get odd results
//otherwise
let ftpHostSpec = new createHostSpecFromEnv("wu1");

//prints the object as best as it can
console.log(ftpHostSpec)

//Calls toString() on the object
//to convert it to a string first
console.log("Spec:" + ftpHostSpec)

satya - 5/26/2019, 4:13:11 PM

how to use require in node.js

how to use require in node.js

Search for: how to use require in node.js

satya - 5/26/2019, 4:16:05 PM

Example


var u = require("ftputils");

satya - 5/26/2019, 4:19:40 PM

How do I throw an exception in Javascript

How do I throw an exception in Javascript

Search for: How do I throw an exception in Javascript

satya - 5/26/2019, 5:01:45 PM

do this


....
throw "any error";
....

try {
....
}
catch (err)
{
   ....
}

satya - 5/26/2019, 5:02:02 PM

how to require global modules in node.js

how to require global modules in node.js

Search for: how to require global modules in node.js

satya - 5/26/2019, 5:21:45 PM

couple of things on modules


//A global module
//Make sure set the global path for 
//NODE_PATH to the directory of node/node_modules
const ftp = require("basic-ftp");

//A local module. notice the ./
const readenv = require("./readenv");

satya - 5/26/2019, 5:24:16 PM

I keep making this mistake


//wrong
const WU1Host = readenv.createHostSpecFromEnv("w_u1");

//when it should be
const WU1Host = new readenv.createHostSpecFromEnv("w_u1");

satya - 5/27/2019, 10:49:53 AM

How do I tell javascript IDEs that a variable is of a certain type?

How do I tell javascript IDEs that a variable is of a certain type?

Search for: How do I tell javascript IDEs that a variable is of a certain type?

satya - 5/27/2019, 10:50:49 AM

when do I add a method to a class vs to the prototype in javascript?

when do I add a method to a class vs to the prototype in javascript?

Search for: when do I add a method to a class vs to the prototype in javascript?

satya - 5/30/2019, 9:17:25 AM

How to format a string message in Javascript

How to format a string message in Javascript

Search for: How to format a string message in Javascript

satya - 5/30/2019, 9:24:38 AM

These are called Template Literals

These are called Template Literals

satya - 5/30/2019, 9:24:46 AM

template literals in javascript

template literals in javascript

Search for: template literals in javascript

satya - 5/30/2019, 9:25:09 AM

Template literals are documented here

Template literals are documented here

satya - 5/30/2019, 9:25:58 AM

How do I tell javascript IDEs that a variable is of a certain type?

You do this hints in comments.

satya - 5/30/2019, 9:26:34 AM

Example


/**
 * @param {FileInfo[]} ftpListingArray 
 * 
 * Each FileInfo looks like this
 * 
 * FileInfo {
    name: '14122018',
    type: 2,
    size: 0,
    permissions: { user: 7, group: 5, world: 5 },
    hardLinkCount: 1,
    link: '',
    group: 'ftp',
    user: 'ftp',
    date: 'Dec 14 2018' }
 *
 *
 */
function workWithListings(ftpListingArray)
{
}

satya - 5/30/2019, 9:28:56 AM

Can you spot the difference: An assert function with template literals


function a(v1, v2) {
    if (v1 === v2){return;}

    //Correct. Notice the back
    console.log(`test failed. ${v1}:${v2}`);
    
    //wrong
    console.log('test failed. ${v1}:${v2}');
    console.log("test failed. ${v1}:${v2}");
}

satya - 5/30/2019, 10:05:57 AM

is there a finally clause in javascript exceptions?

is there a finally clause in javascript exceptions?

Search for: is there a finally clause in javascript exceptions?

satya - 5/30/2019, 10:07:16 AM

Yes finally is available. See here the reference

Yes finally is available. See here the reference

satya - 5/30/2019, 10:07:33 AM

Example


try...catch
try...finally
try...catch...finally

satya - 5/31/2019, 2:47:39 PM

How do I read from a console in node.js

How do I read from a console in node.js

Search for: How do I read from a console in node.js

satya - 5/31/2019, 2:57:25 PM

Readline API is here for node.js

Readline API is here for node.js

satya - 5/31/2019, 3:18:57 PM

Here is an example


var readline = require('readline');

//********************************************
//it will read from command line
//********************************************
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
  });
  


  console.log("rl > Type in a path:");

//********************************************
//line is one of the events
//when the line is done invoke this callback
//See the docs for other events
//********************************************

  rl.on('line', function (line) {
    if (isQuit(line) == true)
    {
//********************************************
//* call rl.close() to stop the events
//********************************************

        console.log("Quitting")
        rl.close();
    }
    else
    {
        //Go ahead and process the line
        processCommandline(line);
         console.log("rl > Type in a path:");
    }
  });

  function isQuit(s)
  {
    const quitLine = s.trim().toLowerCase();
    if ( (quitLine == "q") || (quitLine == "quit"))
    {
        return true;
    }
    return false;

  }

  function processCommandline(line)
  {
     console.log(`Processing ${line}`)
  }