DEV Community

Omar for ClickPesa

Posted on • Originally published at ommyjay.me

3

Using JS Functions Properties in Real Life

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called.

Consider the following logging function below...

function log(message, level) {
  console.log(message);
}

log('just logging...', 1) // "just logging..."
Enter fullscreen mode Exit fullscreen mode

You can use function property to avoid global variables for conditional checking

function log(message, level) {
  if (log.backup) {
    console.log(message);
  }
  console.log(message);
}

//set function property
log.backup = true;

log('backing it up...', 1) // "backing it up..."
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay