JavaScript Hoisting

This topic is very very important in javaScript. It is an unknown behavior of javaScript. Shortly, javaScript compiler moves variables and function declarations

Hoisting is JavaScript’s default behavior of moving declarations to the top of the current scope.

Variables and constants declared with let or const are not hoisted!

JavaScript Declarations are Hoisted

In JavaScript, a variable can be declared after it has been used.

In other words, a variable can be used before it has been declared.

Hoisting applies to variable declarations and to function declarations.

function declarations are hoisted before variable declarations.

Continue reading

JavaScript Functions

I sometimes try to remember basis of JavaScript Functions. So, I often exercise about it. I followed this link https://www.w3schools.com/js/js_function_definition.asp. I copied some important descriptions from that link. My aim is to follow this post when I need to remember again 🙂

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task.

A Function is much the same as a Procedure or a Subroutine, in other programming languages.

Why Functions?

You can reuse code: Define the code once, and use it many times.

You can use the same code many times with different arguments, to produce different results.

JavaScript Function Types

  1. Function Declaration (function statement)
  2. Function Expression
  3. Function Constructor
  4. Self-Invoking Function (Immediately Invokable Function Expression IIFE)
  5. Arrow Function
  6. Object Method Shorthand Definition
  7. Generator Function

JavaScript Function Syntax

Function parameters are listed inside the parentheses () in the function definition.

Function arguments are the values received by the function when it is invoked.

Inside the function, the arguments (the parameters) behave as local variables.

1. Function Declaration (function statement)

When a function declaration is created, a variable named as function name is created as well. This variable is hoisted top of the current scope. In other words, variable is moved to top of current scope. This means that the function can be called before the function declaration

Continue reading

LoopBack 3 – Simple API with Oracle Example

Expose your Oracle Database as a REST service with LoopBack 3 (Node.js Open API Framework)

This post is continuation of previous post https://kenanhancer.com/2019/09/09/loopback-3-simple-api-example/

So if you want to start from beginning, read that one as well.

Follow this link https://kenanhancer.com/2019/09/12/mysql-docker-container-exercise/ to create a Oracle container 🙂

You can find LoopBack 3 project in the following repository. After clonning switch to oracle branch 🙂 or run the following command.

git checkout oracle

https://github.com/kenanhancer/loopback3-simple-api.git

LoopBack 3 – Simple API with Redis Example

Expose your Redis NoSQL as a REST service with LoopBack 3 (Node.js Open API Framework)

This post is continuation of previous post https://kenanhancer.com/2019/09/13/loopback-3-simple-api-with-mysql-example/

So if you want to start from beginning, read that one as well.

You can find LoopBack 3 project in the following repository. After clonning switch to mongodb branch 🙂 or run the following command.

git checkout redis

https://github.com/kenanhancer/loopback3-simple-api.git

LoopBack 3 – Simple API with MongoDB Example

Expose your MongoDB NoSQL as a REST service with LoopBack 3 (Node.js Open API Framework)

This post is continuation of previous post https://kenanhancer.com/2019/09/13/loopback-3-simple-api-with-mysql-example/

So if you want to start from beginning, read that one as well.

Follow this link https://kenanhancer.com/2019/09/13/mongodb-docker-container-exercise/ to create a MySQL container 🙂

You can find LoopBack 3 project in the following repository. After clonning switch to mongodb branch 🙂 or run the following command.

git checkout mongodb

https://github.com/kenanhancer/loopback3-simple-api.git

LoopBack 3 – Simple API with MySQL Example

Expose your MySQL Database as a REST service with LoopBack 3 (Node.js Open API Framework)

This post is continuation of previous post https://kenanhancer.com/2019/09/09/loopback-3-simple-api-example/

So if you want to start from beginning, read that one as well.

Follow this link https://kenanhancer.com/2019/09/12/mysql-docker-container-exercise/ to create a MySQL container 🙂

LoopBack MySQL example will not start wihout running the following code in MySQL Database container. So, run the following command in it.

ALTER USER root IDENTIFIED WITH mysql_native_password BY 'password';

You can find LoopBack 3 project in the following repository. After clonning switch to mysql branch 🙂 or run the following command.

git checkout mysql

https://github.com/kenanhancer/loopback3-simple-api.git

LoopBack 3 – Simple API Example

I am developing APIs with Node.js and LoopBack during last one year. So, it is time to create a post about it. 🙂 I try to build a detailed project. I hope that this post will be a good reference for developers.

Install LoopBack CLI Tools

There are two LoopBack CLI Tools.

You can also use the legacy StrongLoop CLI tool slc

Install LoopBack CLI Tool

$ npm install -g loopback-cli

This installs the lb command-line tool for scaffolding and modifying LoopBack applications.

Continue reading

Node.js babel different usage transpiling to ES5 and Debugging

There are different usages of babel tool. I put a sample package.json file. Notice that there are scripts block which has different usages.

Creating Babel configuration file

.babelrc

{
  "presets": [
    "env"
  ],
  "plugins": [
    "transform-runtime"
  ],
  "sourceMaps": true,
  "retainLines": true
}

Running with Babel-node

//If you have installed babel-cli package globally, run below code.
babel-node --presets=env --plugins=transform-runtime src

or

//If you have .babelrc config file, run below code without specifying inline arguments.
babel-node src

or

//If you have installed babel-cli package locally, run below code.
./node_modules/.bin/babel-node --presets=env --plugins=transform-runtime src

Running with Babel-register

node --require ./node_modules/babel-register src

or

node --require babel-register src/index.js

Running with Babel

babel ./src -d dist -s && node dist/index.js
{
  "name": "babeltest1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "buildv1:src": "babel ./src -d dist -s",
    "buildv1": "rm -rf dist && npm run buildv1:src",
    "start:buildv1": "npm run buildv1 && node dist/index.js",

    "buildv2:src": "babel ./src --out-dir dist --source-maps",
    "buildv2": "rm -rf dist && npm run buildv2:src",
    "start:buildv2": "npm run buildv2 & node dist/index.js",

    "buildv3:src": "babel --no-babelrc . --out-dir dist --presets=env --plugins=transform-runtime --source-maps --ignore 'node_modules,dist,.babelrc,.compiled,.vscode'",
    "buildv3": "rm -rf dist && npm run buildv3:src && mv dist/src/* dist && rm -rf dist/src",
    "start:buildv3": "npm run buildv3 && node dist/index.js",
    
    "buildv4:src": "$(which babel) --no-babelrc src --out-dir dist --presets=env --plugins=transform-runtime --source-maps",
    "buildv4": "rm -rf dist && npm run buildv4:src",
    "start:buildv4": "npm run buildv4 && node dist/index.js",

    "buildv5:src:watch": "babel src --out-dir dist --source-maps --watch",
    "buildv5:watch": "rm -rf dist && npm run buildv5:src:watch",
    "start:buildv5": "npm run buildv5:watch && node dist/index.js",

    "buildAndRunWithBabelNode": "babel-node --presets=env --plugins=transform-runtime src",

    "buildAndRunWithBabelRegister": "node --require ./node_modules/babel-register src",

    "buildAndRunWithBabelRegisterv2": "node --require babel-register src/index.js",

    "build:lib": "$(which babel) --no-babelrc src --out-dir=dist --presets=env --plugins=transform-runtime --copy-files --source-maps",
    "build": "rm -rf dist && npm run build:lib",
    "watch": "npm run build:lib -- --watch",
    "start": "npm run build && node --inspect dist/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.6.1"
  },
  "dependencies": {}
}

You can reach github project from the following link.

https://github.com/kenanhancer/BabelDifferentUsageDemo1