JavaScript

node notes: require() caches module.exports

Posted on Updated on

if you are importing a file that instantiates a new object

when you import the file again as a 2nd object you would expect … a new object

but your 2nd variable will be the same object as the first variable, even though a “new” constructor was used. why?

Node caches module imports , it wont load the file again so no new instance!!!

example:

// file greet3.js
function Greetr(){
  this.greeting=”Hello World from greet3″
  this.greet=function(){console.log(this.greeting)}
}
module.exports = new Greetr();

 

//app.js
var greet3 = require(“./greet3.js”);
greet3.greet();
greet3.greeting = “NEW GREETING!!!!”
var greet3a = require(“./greet3.js”);
greet3a.greet();
//console.log
Hello World from greet3
NEW GREETING!!!!
how to resolve??
in the file being exported, greet3.js, send the constructor not the new instance
//greet3.js
module.exports = Greetr;
//app.js
var Greet3a = require(“./greet3.js”);  //you capitalize greet3a to tell its constructor
var grtr = new Greet3a();
Advertisements

Node notes : on require with no js file in local path

Posted on

if you require, for example  “greet”, and greet.js is not found, node will look for folder called “greet” and then look for a file called index.js

 

 

Errors encountered on my way to install Node.JS v8 on ubuntu

Posted on

when running apt-get install nodejs

You might want to run ‘apt-get -f install’ to correct these:
The following packages have unmet dependencies:
libgail-3-0 : Depends: libgtk-3-0 (= 3.20.9-1ubuntu2) but 3.18.9-1ubuntu4 is to be installed
nodejs : Depends: libuv1 (>= 1.6.1) but it is not going to be installed
python3-gi-cairo : Depends: python3-gi (= 3.22.0-1) but 3.20.0-0ubuntu1 is to be installed E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).

to fix:  apt-get -f install

When trying to install nodejs again will only allow for node-legacy (v4)

to fix: 

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

 

finally at v8 locally

 

io.emit vs socket.emit

Posted on

io.emit lets the server send an event to all the connected clients at once. This is useful when you need to send some data to everyone. This is something we want everyone to be able to see.

socket.emit sends an event to a single socket. This is great when you need to send an event to a single user. Example: This is why we use socket.emit when sending out a greeting message. It only needs to go to that one user.

 

socket.io notes

JavaScript Classes review

Posted on Updated on

Some basic notes

// *****START CLASS***********************************************

//The constructor function is called automatically when an instance of the class is created.
// a special method for creating and initializing an object created with a class.
// There can only be one special method with the name “constructor” in a class.
class Person {
constructor (name,age) { //this is a function
// console.log(name,age)
this.name=name;
this.age=age;
}
//methods
getUserDescription(){return `${this.name} is ${this.age} year(s) old`}
}

// *****END CLASS***********************************************

var me = new Person(“Andrew”,25); //arguments are passed to constructor function
// console.log(“this.name”, me.name)
// console.log(“this.age”, me.age)
var description = me.getUserDescription(); // Andrew is 25 year(s) old
console.log(description);

Location object

Posted on Updated on

Contains info about the URL

Part of the window object, also accessible by document object

Can access in dev tools by typing location

Items in location

  • search. : that is the query string 
  • pathname 
  • origin 
  • href 
  • port

See MDN for more
Examples: access seatch use window.location.search
See stevenbenner jQuery deparam for deconstruction of params  or go here 

Example deparam after loading function 

JQuery.deparam(window.location.search)

What is the end goal? Musings on programming.

Posted on

I a doing FreeCodeCamp’s Build a Weather app for the 2nd time. (maybe 3rd? who knows)

This time I thought I would get fancy, try to get the location from HTML5 first, then if an error, get the location from a IP geolocation service.

Well, I am finding that some functions don’t wait for the return of data from another, and rather than having an orchastrator function I would need to chain the functions together.

example:

Original:  master() call geoHTML5; if error master() call geoIP;

now: master() call geoHTML5 call geoIP() call….

Now I could just forgo HTML5 geolocation since IP geolocation works and actually has better data than HTML5 gelocation.

What is my end goal? To finish the project? Then ip geolocation would be the right choice, dont waster time on other things.

But what is my REAL end goal? TO get better at programming and learn the ins and outs of javascropt. That means I need to deal with my original plan and found out how to implement it..

What is the end goal? I’ve already done a weather app. The goal is to get better at programming.