SaaS News Hubb
Advertisement
  • Home
  • News
  • Software Engineering
  • Software Development
  • SAAS Applications
  • Contact Us
No Result
View All Result
  • Home
  • News
  • Software Engineering
  • Software Development
  • SAAS Applications
  • Contact Us
No Result
View All Result
SaaS News Hubb
Home Software Development

JavaScript Class Privates

by admin
April 6, 2022
in Software Development
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter

One of my aspects of JavaScript that drew me to it as a young developers was that its syntax was loose and I could code quickly. As you gain experience as an engineer, you start to realize that some traditional coding structure is a good thing, even if it slows you down. Using Jest or TypeScript to add typing to your JavaScript can save you from maintenance headaches and unexpected errors, for example. While those are pre-compile tools to accomplish structure, we’ve traditionally employed vanilla JavaScript patterns to mock private variables and methods in JavaScript.

Did you know, however, that browsers and the JavaScript language support a specific syntax for creating private variables and functions in classes? Let’s have a look!

Properties and methods on a class have always been considered public; to make a property or method private, add a # at the beginning of their name:

class Developer {
  name;
  #age; // Don't tell anyone my age!

  constructor(name, age) {
    this.name = name;
    this.#age = age;
  }
};

const David = new Developer('David', 38);

console.log(David.name); // David
console.log(David.age);  // undefined
console.log(David.#age); // Error!  Uncaught SyntaxError: Private field '#age' must be declared in an enclosing class

David.name is available because name is public, while age is private because it’s declared with a #. Similarly we can declare a private method with #:

class Developer {
  name;
  #age; // Don't tell anyone my age!

  constructor(name, age) {
    this.name = name;
    this.#age = age;
  }

  #getAgeInDogYears() {
    return this.#age * 7;
  }
};

getAgeInDogYears is only allowed to be called from within the class itself due to being declared with #. We can expose any information from within the class, public or private, if we make it available by public method:

class Developer {
  name="";
  #age = 0;
  #ageInDogYears = 0;

  constructor(name, age) {
    this.name = name;
    this.#age = age;

    this.#ageInDogYears = this.#getAgeInDogYears();
  }

  #getAgeInDogYears() {
    return this.#age * 7;
  }

  log() {
    console.log(this.name);
    console.log(this.#age);
    console.log(this.#ageInDogYears);
  }
};

const David = new Developer('David', 38);
David.log();

// David
// 38
// 266

Adding a native syntax for declaring private class properties and methods is a welcomed addition to JavaScript; even better is that you can do so by simply adding a # to the beginning of its name.

Have you written code using private syntax in JavaScript? How was the experience?!

Website performance monitoring
Website performance monitoring


Source link
Previous Post

Perspectives in Leadership: Diversity, Equity and Inclusion

Next Post

Building Infrastructure Platforms

Related Posts

Software Development

What “The Great British Baking Show” teaches us about teamwork in software development

May 15, 2022
Software Development

Ping vs Traceroute: How to Troubleshoot Your Connections

May 14, 2022
Software Development

10 Free Portfolio & Lookbook Templates for Adobe InDesign

May 14, 2022
Software Development

How to Profile Python Memory Usage | Pluralsight

May 13, 2022
Software Development

Weekly News for Designers № 643

May 13, 2022
Software Development

AWS re:Invent, Pluralsight and AWS are helping build the future of cloud learning

May 12, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Popular

Software Engineering

Snyk Engineering with Guy Podjarny

May 15, 2022
Software Development

What “The Great British Baking Show” teaches us about teamwork in software development

May 15, 2022
SAAS Applications

Forum Post: Auditing feature

May 15, 2022
Software Engineering

Software is adopted, not sold (Ep. 441)

May 14, 2022
Software Engineering

Data Delivery with Naqeeb Memon

May 14, 2022
Software Development

Ping vs Traceroute: How to Troubleshoot Your Connections

May 14, 2022
Software Development

10 Free Portfolio & Lookbook Templates for Adobe InDesign

May 14, 2022
SAAS Applications

CRM reporting Extension – SSRS instance is blank

May 14, 2022
SAAS Applications

Greenshades Selects Branch as Exclusive Banking and Card Solution

May 14, 2022

© 2022 Sass News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy

Navigate Site

  • Home
  • News
  • Software Engineering
  • Software Development
  • SAAS Applications
  • Contact Us

Newsletter Sign Up

No Result
View All Result
  • Home
  • News
  • Software Engineering
  • Software Development
  • SAAS Applications
  • Contact Us