What are the benefits of defining a function outside of an exported class in a JavaScript library to create private functions?

After experimenting with creating my own Javascript library, I am contemplating the best approach.

I recently discovered a convention for implementing private functions within a class by prefixing them with an underscore _, yet they are still accessible. The code example looks like this:

export default class Test {
  constructor() {
    this._privateFunction();
  }
  _privateFunction() {
    ...
  }
}

Instead of following this convention, I am considering placing functions outside the exported class. What are your thoughts on this possible solution?

export default class Test {
  constructor() {
    privateFunction();
  }
}

function privateFunction() {
  ...
}

In my research, I have not found a way to access functions declared outside of the exported class, making it appear as a feasible alternative.

Do you think this is a good idea or could it potentially create issues with browser parsing?

Answer №1

Absolutely, utilizing closures to maintain privacy, even for functions, is a widely adopted and conventional practice. There are no issues with handling this.

One drawback of this method is that the private function is treated simply as a function rather than a method. This implies that you must explicitly pass the instance as an argument in order to access it:

export default class Test {
  constructor() {
    privateFunction(this);
  }
}

function privateFunction(self) {
  …
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting: Why is the AngularUI Modal dialog malfunctioning

I'm currently working on integrating an angularUI modular dialog into my application. Here is a snippet from my controller.js file: define([ 'app' ], function(app) { app.controller('TeacherClasses', [ '$scope', &apo ...

No entries found in the Nuxt/content module's array

<template> <div> <input v-model="searchQuery" type="search" autocomplete="off" placeholder="Search Articles" /> <ul v-if="articles.length"> ...

Function to save prices as cent-based figures in Javascript using Regex

Trying to extract prices from a string using regex can be tricky, as unexpected issues may arise. For example, obtaining the following values: US$1234.56 $12 $12.34usd $0.56 .56 dollars and converting them to: 123456 1200 1234 56 56 is necessary for s ...

Navigating through events within React

I'm working on creating a login page, where upon clicking the submit button it should navigate to the Dashboard page. However, I seem to be encountering an issue with my code. After entering login details and clicking on the login button, the details ...

Tips for creating visually appealing tables in ReactJS

For my school project, I need to create a table using ReactJs. I have already created the table but it needs improvement in terms of design. However, I am unsure how to modify my code to achieve the desired look. I tried looking on YouTube for tutorials, b ...

Error: Expecting only one React element child to be passed into React.Children.only() function

I am encountering an issue while attempting to construct a web table using the antd library. The exact error message reads: "react.development.js:1251 Uncaught Error: React.Children.only expected to receive a single React element child". I have been stru ...

Firebase's equalTo function seems to be malfunctioning

Having encountered an issue with Firebase, I am currently attempting to fetch all of my posts in JavaScript. Specifically, I am looking for posts in the correct language that are marked as "published" and sorted by their published date. In my Firebase dat ...

Between the transition from Android 4.0 to 4.3 (inclusive), there is a known issue where web storage is lost when navigating between web

I'm currently working on an Android project that heavily relies on the WebView to navigate through multiple HTML pages stored on the device. The inputs entered on these pages are then submitted to the WebView for storage in a database when necessary. ...

Angular JS is failing to trigger the change event for a checkbox that has already been checked

Be sure to take a look at this link. I am noticing some strange behavior with Angular's on change event when dealing with an initially checked checkbox. Interestingly, the jQuery event seems to work fine in this situation. The angular event only trigg ...

Challenges in managing click events

I am facing an issue with my Jquery UI resize handler. It is positioned absolutely over a div that contains a set of LI's (in this case, a set of dates). The problem is that when I click on any date, the click event is not being propagated because the ...

Ways to extract information from a URL when utilizing JQuery code

Code snippet: $(document).ready(function(){ $(".uni_type").click(function(){ uni_type = this.id; location.href = "filter-colleges.php?uni_type="+uni_type; }); }); HTML code: <ul> <li class="uni_type" id="central univ ...

Using Three.js to transfer one object's rotation to another object

I have been attempting to transfer one object's rotation to another with no success using the following methods: //The first method rotates much faster than the original object's rotation boxme1.rotateOnAxis(new t.Vector3(0,1,0), cube.rotation.y ...

Using node modules within an HTML document

I'm finding it challenging to understand how npm handles dependencies when it comes to referencing them in HTML. For example, if I have a specific version of a plugin installed that includes the version number in its path or file name, and npm is set ...

jquery mobile and cordova causing issues with refreshing header in collapsible listview

My current project involves developing an app using Cordova and JQuery Mobile 1.4.3 that retrieves data from a webservice. After loading Cordova, I face no issues fetching the data and updating the DOM. The retrieved data is displayed in a collapsible lis ...

Utilize hosted jQuery in HTML on IIS or locally: Which is better?

Currently, I am facing an issue with embedding hosted jQuery in my web application. When using the "hosted" version of jQuery from an external URL like this: <script src="http://code.jquery.com/jquery-latest.js"></script> The script doesn&apo ...

What could be causing the delay in Handlebars.js binding process after the initial run?

I'm facing a puzzling issue with my Handlebars.js template. Initially, it efficiently renders data into a table without any hiccups. However, subsequent runs take an unexpectedly long time (even with the same data!) and sometimes result in browser cra ...

Issue with JavaScript functionality after relocation to an external .js file

Initially, I had a JavaScript code in my HTML page. However, I decided to move the script to an external JavaScript file named jful.js. <script type="text/javascript> $(function() { // Determine the initial top offset of the navigation bar ...

Subtracting Arrays Containing Duplicates

Imagine having two arrays defined like this: const A = ['Mo', 'Tu', 'We', 'Thu', 'Fr'] const B = ['Mo', 'Mo', 'Mo', 'Tu', 'Thu', 'Fr', 'Sa&ap ...

The responsive dropdown menu is failing to show the elements within the array

My website has a responsive navbar that updates values based on selected tabs. https://i.sstatic.net/Fl6tM.png Now, I'm trying to replicate the same functionality in mobile view by using a select menu instead of a navbar. https://i.sstatic.net/JGrH ...

Order a portion of a JSON array according to another part of the same array

Having a json array that needs sorting in JavaScript. The EventName field should match the respective Age fields like 01-10 Days and 10-20 Days. [ {Age: "01-10 Days", EventName: "Invoice AP Review", Value: 1, ActiveInvoices: []} ,{Age: "01-10 Days", Even ...