Cookie Consent by Free Privacy Policy Generator 📌 Revisiting the "Revealing Module pattern"


✅ Revisiting the "Revealing Module pattern"


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

(Cover Image source)

Maybe you've heard of the "Revealing module pattern" [RMP], which is a way to create protected code modules in Javascript. Unlike JS class objects, code inside the modules cannot be altered from the outside, which can be a huge benefit to protect your code from it´s worst enemy: you yourself! There are quite some explanations on the pattern on the net (even on dev.to), but I like to show some extensions to make the patter more useful.

The RMP uses the fact that local functions and variables created inside a function cannot be reached from the outside. The function body forms a local scope, that is insulated from the rest of the program. To make elements inside the function body available, the main function returns an object containing the all referencs that should be accessible. See an example here:

function Person(myName) {
  let name = myName

  function log(txt) { console.log(txt) }
  function public_talk() { log(name + " is talking") }
  function public_dance() { log(name + " is dancing") }

  // interface
  return {
    talk: public_talk,
    dance: public_dance
  }
}

let father = Person("Peter")

father.talk()
father.dance()

Person returns an object contaning just the two functions talk and dance, so all the "internals" are hiden to the outside world. In contrast to JS classes, the code inside an RMP looks pretty normal, in fact the RMP-body looks ans works exactly like a small program inside the rest of the code. But there are some downsides too. Most of all, they lack any form of inheritance, so the are not open for extensions. But let´s see, what we can do with the RMP...

Tricks and shortcuts

In the examples below I will use some JS-"tricks" to improve the RMP and make it more useful. Here are some elements I use:

// Destructuring
let {a, b} = myFunction()
-> if myFunction returns an object {a:1, b:2, c:3}, a and b are assigned to local variables.  

// Arrow-functions
function value(){return x}
value = () => x
-> Shorter, if you just need to return a value

// ES6-shorthands
let a=1, b=2
let ob = {a:a, b:b}
-> instead, you can just write ob = {a,b}

// getter and setter
let a=1, b=2
let ob = {
  get a(){ return a}, 
  set a(x){ a = x },
  b
}
-> setter can be used like a normal proerty ob.a = 5, but invokes a function call

This "tricks" are important to know to understand the following code examples.

Improving the Revealing Module Pattern

First, let see if we can make our example a bit smarter:

function Person(name) {
  function log(txt) { console.log(txt) }
  function talk() { log(name + " is talking") }
  function dance() { log(name + " is dancing") }

  // interface
  return {
    talk,
    dance
  }
}

let father = Person("Peter")
...

We do not need a variable to keep the name, as name is already a local variable in the function scope. So Parameters do not need to be stored and can be altered too. And we do not need separate names for our functions using ES6-shorthands.

Removing limitations

But what about changing values from the outside? Ok, you can build a function to to the job. But there are better ways. Let´s try to expose a "variable":

function Person(name) {
  function log(txt) { console.log(txt) }
  function talk() { log(name + " is talking") }
  function dance() { log(name + " is dancing") }

  // interface
  return {
    name,
    talk,
    dance
  }
}

let father = Person("Peter")
father.talk() // --> Peter is talking
father.name = "Paul"
father.talk() // --> Peter is talking

This does not work, as the object just returns a copy of our value. Even if we change the name, this would not be reflected in the result.

But you can use getters and setter to help:

// interface
  return {
    get name(){return name},
    set name(x){name = x},
    talk,
    dance
  }

  console.log(father.name)

Both options have their advantages, especially if you want a two way binding:

// interface
  return {
    get name(){return name},
    set name(x){name = x},
    talk,
    dance
  }
  console.log(father.name)

// interface
  return {
    name: (x) => x ? name = x: name,
    talk,
    dance
  }
  father.name()
  console.log(father.name())



...

✅ Revisiting the "Revealing Module pattern"


📈 57.56 Punkte

✅ The Revealing Module Pattern in JavaScript


📈 37.55 Punkte

✅ Revisiting the Office of Legal Counsel’s Override Opinion


📈 20.01 Punkte

✅ Revisiting the OnePlus One in 2024! 😱


📈 20.01 Punkte

✅ 10 older Xbox games worth revisiting in 2019


📈 20.01 Punkte

✅ Rogue RDP – Revisiting Initial Access Methods


📈 20.01 Punkte

✅ This October, Revisiting Our Shared Responsibility To Stay Safe Online


📈 20.01 Punkte

✅ Revisiting Apple Notes (6): The Protobuf


📈 20.01 Punkte

✅ HTC One M8: Revisiting the 2014 GOAT 🐐


📈 20.01 Punkte

✅ Revisiting General Counsel Ney’s Speech in Light of New Pentagon Leadership


📈 20.01 Punkte

✅ Revisiting site search + SQLite as a search engine


📈 20.01 Punkte

✅ Shamoon malware revisiting Saudi Arabia; cyberinfrastructure on high alert


📈 20.01 Punkte

✅ Revisiting Export Controls in the COVID Era


📈 20.01 Punkte

✅ Revisiting Insecure Direct Object Reference (IDOR)


📈 20.01 Punkte

✅ Revisiting the Risk Management Framework in Light of Revision 2


📈 20.01 Punkte

✅ Revisiting Heaven's Gate with Lumma Stealer


📈 20.01 Punkte

✅ Revisiting stable-kernel regressions [LWN.net]


📈 20.01 Punkte

✅ Revisiting the HTML Problem Space and Introducing OOHTML


📈 20.01 Punkte

✅ Revisiting Carter Page


📈 20.01 Punkte

✅ AppStories, Episode 321 – Revisiting RSS


📈 20.01 Punkte

✅ [$] Revisiting stable-kernel regressions


📈 20.01 Punkte

✅ Revisiting International Law in the Gaza Context


📈 20.01 Punkte

✅ Revisiting the Jobs Artificial Intelligence Will Create


📈 20.01 Punkte

✅ Revisiting 2b2t Tamed Animal Coordinate Exploit


📈 20.01 Punkte

✅ Revisiting Python: A New Chapter in My Learning Journey


📈 20.01 Punkte

✅ Revisiting the abbr element


📈 20.01 Punkte

✅ Revisiting Haskell after 10 years


📈 20.01 Punkte

✅ Revisiting the Mladić Trial Amidst Trump Admin’s Attacks on International Criminal Justice


📈 20.01 Punkte

✅ ELF Section Docking: Revisiting Stageless Payload Delivery


📈 20.01 Punkte

✅ Revisiting Non-separable Binary Classification and its Applications in Anomaly Detection


📈 20.01 Punkte

✅ Revisiting Glupteba: Still Relevant Five Years after Debut


📈 20.01 Punkte

✅ Revisiting the Death of Data Science


📈 20.01 Punkte

✅ [$] Revisiting PEP 394


📈 20.01 Punkte

✅ Revisiting Android on Linux


📈 20.01 Punkte

✅ Revisiting the "Rules of React"


📈 20.01 Punkte











matomo

Datei nicht gefunden!