Skip to content

Instantly share code, notes, and snippets.

@FaisalAl-Tameemi
Last active October 7, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FaisalAl-Tameemi/3f3d61c01303b227a43d233241227217 to your computer and use it in GitHub Desktop.
Save FaisalAl-Tameemi/3f3d61c01303b227a43d233241227217 to your computer and use it in GitHub Desktop.
JS Fundamentals Review + NodeJS Modules Review / Examples.

JS Foundations

Topics:

  • Scopes in JS
  • Context (aka this) in JS
  • Callbacks Review
  • NodeJS Modules
  • NodeJS Automated Testing w. Mocha - time permits

Setup:

  1. Create the following skeleton of files:
./01-scope.js
./02-context.js
./03-callbacks.js
./04-modules.js
./lib
./lib/util.js
./README.md
  1. Create a package.json file by running npm init -y in your terminal

  2. Remember to include use strict; at the top of all of your files

Strict mode helps out in a couple ways:

It catches some common coding bloopers, throwing exceptions. It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object). It disables features that are confusing or poorly thought out.

Read Full Article

// global scope start
'use strict';
var name = 'Faisal';
var printSomething = function(data){
// local scope 1 start
var delay = function(){
// local scope 2 start
var data_inside_delay = 5;
console.log('...')
console.log('...')
console.log(data);
// local scope 2 end
}
console.log(data_inside_delay);
delay();
// local scope 1 end
}
printSomething(name);
// global scope end
'use strict';
const greeting = function(){
console.log("Hi my name is " + this.name);
}
const people = [
{
name: 'Faisal',
age: 23,
greeting: greeting
}, {
name: 'ABC',
age: 99,
greeting: greeting
}
]
people[0].greeting();
'use strict';
const people = [
{
name: 'Faisal',
age: 23
}, {
name: 'Jane',
age: 45
}, {
name: 'ABC',
age: 99
}
]
const filter = function(data, _cb){
const output = [];
// for each element in data
data.forEach(function(elm){
// check if this element is to be included in the output
if(_cb(elm)){
output.push(elm);
}
});
// once all elements have been checked
return output;
}
const peopleFilterFunc = function(person){
return person.name.length <= 4;
}
const under_50 = filter(people, peopleFilterFunc);
console.log(under_50);
{
"name": "js-foundations",
"version": "1.0.0",
"description": "",
"main": "01-scope.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment