Sharing Top Content from the Angular-sphere.

Using WeakMap for Private Properties • Chris Ng

  • In the example below, we pass in an empty object into the WeakMap to hold all of the private properties of the class Wizard .
  • In doing so, keeping the namespace hidden from all functions except members of the class effectively implements private properties.
  • A private property is a property that is only accessible to member functions of instances of the same class.
  • To store the private properties, we also pass in the reference to the unique this object of the instance of the class Wizard as a key to the WeakMap.
  • const myWeakMap = new WeakMap(); const harry = {}; const potter = () => {}; myWeakMap.set(harry, “cat”); myWeakMap.set(potter, 7); myWeakMap.has(harry); // true myWeakMap.get(potter); // 7 myWeakMap.delete(harry);

Read the full article, click here.


@chrisrng: “Read about using WeakMap for JS Private Props: #js #javascript #es6 #weakmap #webdevelopers #angular #react”


This blog is about a cool idea of leveraging WeakMap to have private properties in Javascript Classes. A private property is a property that is only accessible to member functions of instances of the same class. Javascript inherently does not… | Chris Ng | is a full stack developer specializing Vanilla JavaScript and CSS, always looking to collaborate!


Using WeakMap for Private Properties • Chris Ng

Comments are closed, but trackbacks and pingbacks are open.