Javascript journal

How do I compare two javascript object references?

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

Comparison operators in JS


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

The 2 equals will coerce types

Both have to be of the same type

Contents must match

Important thing is native types in JS are NOT objects

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

How can I print an object reference in Javascript?

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


Boolean
null
undefined
String
Number

Array
Function
Object

Here is that question on SOF

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!!!

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

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

How to read environment variables in Javascript

Search for: How to read environment variables in Javascript

That is talked about here in SOF


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

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

Notice the alternate syntax to access javascript arrays

The toString function for javascript objects

Search for: The toString function for javascript objects

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


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)

how to use require in node.js

Search for: how to use require in node.js


var u = require("ftputils");

How do I throw an exception in Javascript

Search for: How do I throw an exception in Javascript


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

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

how to require global modules in node.js

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


//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");

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

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

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?

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?

How to format a string message in Javascript

Search for: How to format a string message in Javascript

These are called Template Literals

template literals in javascript

Search for: template literals in javascript

Template literals are documented here

You do this hints in comments.


/**
 * @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)
{
}

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}");
}

is there a finally clause in javascript exceptions?

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

Yes finally is available. See here the reference


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

How do I read from a console in node.js

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

Readline API is here for node.js


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}`)
  }