NodeJS and Javascript

NodeJS and Javascript

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

JS EventEmitter Constructor

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (22)

Section 1

(22 cards)

JS EventEmitter Constructor

Front

function Emitter() { this.events[]; } The emitter has an array of events it can respond to.

Back

Streams

Front

How a large amount of data can be dowloaded and processed. The buffer is used to gather enough data to continue processing that data.

Back

Javascript Aside: By Reference and by Value

Front

When passing primitive values to a method parameter, JS will make a copy of that primitive in another location in memory. For object literals, it will not make a copy. The functions behavior will change the object. Objects are passed by reference.

Back

Javascript Aside: Objects and Object Literals

Front

JavaScript Object: Is a Collection of Name Value pairs. Each value may be other Name:Value pairs Object Literal: you can wrap name:value pairs in curly braces and also include functions in that name value pairs. This way you can combine attributes and behavior.

Back

JavaScript Aside: IIFE- Immediately Invoked Function Expressions

Front

If you wrap a function definition in parens, you can immediately invoke it. (function(){ var firstName='John'; console.log(firstname);})(); This was a way to protect outside scope from naming collisions. This was how to fake modular code before that came in later versions of Javascript.

Back

Javascript: First Class functions and expressions

Front

JavaScript has First Class functions: Everything you can do with other type, you can do with functions. You can store functions in variables, pass them as parameters, ect. Functions in Javascript are Objects

Back

Node's JavaScript Core

Front

Node contains a lot of Javascript files that are wrappers of C++ code, and is then being used to perform some additional feature. For example, Node's zlib javascript file wraps C++'s function to access zip files. Node also contains straight JS utils and such that do not invoke C++ code.

Back

Let's Build a Module

Front

require() is a function that can import another javascript file. You can use exports() to make the code from within that JS file to the outside program. This explicit exports() method helps protect naming collisions and such.

Back

libuv C++ library

Front

Asynchronous C++ library used by Node. It has an event loop that is constantly running and checking for events that need to be run and executed.

Back

An expression is

Front

A block of code that results in a value

Back

Events in Node.js

Front

System Events triggered by C++ core (in libuv) (file opened, task complete, ect) Custom Events (JS core) Event Emitter. This often wraps calls from C++ libuv

Back

accessing members of an object literal

Front

var person = { firstname: 'John' } person.firstname or person['firstname'] <-dynamic

Back

JavaScript and Inheritance

Front

Prototypical Inheritance. Moving down the Prototype Chain via proto{} to access that object's members.

Back

module.exports and require

Front

When you use module.exports, it will actually read the contents of your imported file, and wrapped into a function expression. That is how it protects the outside scope.

Back

Node can access files

Front

Node's C++ core has global modules for Streams and Buffers can process raw binary data and this is how Node can access files.

Back

Function Constructors and Prototype Chain

Front

A normal function that is used to construct objects. function Person(first,last){ this.first=first; this.last=last; } Person.prototype.greet = function(){ console.log('hello, ' + this.first); } var john = new Person('jim','doe'); The greet method is available to all instances of Person and are the same object.

Back

How is Node.js Asynchronous? aka Event Driven Non-Blocking I/O in V8 JavaScript

Front

V8 engine is synchronous, but the libuv module in Node can make reqs from the OS and listed from posted events that are placed in its queue. libuv processes these events in an event loop, and then invokes callbacks that are executed by the V8 engine.

Back

JS Event Emitter - accepting added listeners

Front

Emitter.prototype.on = function(type,listener){ this.events[type]=this.events[type]||[]; this.events[type].push(listener); } You can use this method to push new listeners onto an Event Emitter to handle events.

Back

Module

Front

Reusable block of code to allow us to make code modular. Node invented commonjs modules to create a standard for how code modules should be structured

Back

JS Event Emitter - triggering listeners

Front

Emitter.prototype.emit = function(type){ if (this.events[type]){ this.events[type].forEach( function(listener){ listener(); }); }} this is an example of how an Event Emitter can trigger added listeners based on events posted to it

Back

What does Javascript need to be a Server?

Front

1.) Ways to reuse and organize code 2.) File I/O Access 3.) Ways to communicate with databases 4.) The ability to Communicate over the internet 5.) The way to send and receive requests 6.) The way to deal with long running work

Back

JavaScript Aside: Javascript is Syncronous

Front

The V8 engine runs synchronously, but Node.js is asyncronous

Back