Unit 0: Introduction

The learning content is split across several units. Each of them is focused on a key concept or functionality for the app. In this unit you will learn the fundamentals for oneJS programming. If you are an advanced JavaScript developer and you are familiar with Webpack and Expo, you can skip this unit.

Functions

A JavaScript function is a block of code designed to perform a particular task. Function parameters are listed inside the parentheses () in the function definition. In JavaScript there are two ways of defining functions:

Function Keyword

function add(x, y) = {
return x + y;
}

Arrow Function

const add = (x, y) => {
return x + y;
}

The arrow function is the preferred notation as it more compact in complex scenarios. When a JavaScript function reaches a return statement, the function will stop executing.

Functional Programming

How are functions related to functional programming? FP is a programming paradigm where programs are constructed by applying and composing functions. In functional programming, functions are treated as first-class citizens, meaning that they can be bound to names, passed as arguments, and returned from other functions, just as any other data type can. Functional programming is sometimes treated as synonymous with purely functional programming, a subset of functional programming which treats all functions as deterministic mathematical functions, or pure functions.

Pure Functions

Pure functions are a subset of functions that possess a set of unique properties:

Referential Transparency

Pure functions always produce the same output for same arguments irrespective of anything else. This is true for oneJS. Components are defined as functions and if they are called with the same parameters (i.e. component properties), they will look and behave the same way.

No Side Effects

Pure functions do not modify any arguments, local/global variables or input/output streams. This is only partially true for oneJS. An app composed uniquely of strictly pure functions would be completely static as no side effects means no changes in the app’s state. In oneJS, we follow what we call the No Implicit Side Effect philosophy. Functions will never modify external state implicitly in their definition. This would break function’s isolation, as the content of the function would need to be aware of its exterior. In oneJS, a function can only modify the state of the application if it is explicitly forced to do so when it is called. You will find how to do so in Unit 2: State.

Why Functional Programming?

Modular

Since every function only depends on its input arguments, they enable loosely coupled architectures like modular design. This increases increases overall productivity. Small modules can be coded quickly and have a greater chance of being re-usable. Moreover, modules can be tested separately which helps you reduce the time spent on unit testing and debugging.

Maintainability

Functional programming is easier to maintain as you don’t need to worry about accidentally changing anything outside the given function.

Project Structure

One of the advantages of oneJS is being able to use a single codebase for web and native apps. The first step is to set up your development environment using the app creator as indicated in the get started page. This creates a template project with the predefined structure described below:

By default, oneJS uses bundling tools: A bundler is a tool that packages your code and all its dependencies together in a single JavaScript file. There are several bundlers you can choose from. Out of the box, oneJS uses Webpack for the web and Expo for iOS and Android. It is not the scope of this website to describe in detail the functionality of these products, but if you need more information about the tools that power oneJS, make sure to refer to the following links: