Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-private-properties

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-private-properties

Provide private variables using WeakMaps

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

babel-plugin-transform-private-properties

Compile private variables into classes utilizing weak maps.

Installation

$ npm install babel-plugin-transform-private-properties

Usage

.babelrc

{
  "plugins": ["babel-plugin-transform-private-properties"]
}

Via CLI

$ babel --plugins babel-plugin-transform-private-properties script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["babel-plugin-transform-private-properties"]
});

Implementation

class TestClass {

    @Private
    p=1; // Decalare private var. Very important, @Private is on it's own line, and is capitalized.

    @Private
    s;

    // Declare non-private class property.
    j=2;
    constructor() {
        this.p = 2; // Setting private.

        var self = this // Yes, we follow copying this scope.
          , other="gone";

        self.p = this.p.g; // Setting private (p) self is assigned to "this" so assign p to value of p.g

        this.func1("constructor",this.p); // Call func1 (which is private) with options. Can't call fun1 outside of this class.
 
        var me;
        me = self; // Yes, we follow copying this scope too.

        me.s = 5;

        me.p = 7;

        me.j = 8;

        self = me;

        self.p = me.p;

    }

    // You can create public functions with same name as private variable as a way to access the private in a controlled way.
    p() {
        return this.p;
    }

    // Getters can be the same as a private variable to gate-keep access.
    get s { // this probably shouldn't be allowed to work.
	    return this.s;
    }

    @Private
    func1(fromWhere) {
        console.log("This is the first function called from ",fromWhere);
    }
}

This results in the usage such as:

var t = new TestClass();
t.func1("Outside"); // t.func1 is not a function
t.s = "hi" // TypeError: Cannot set property s of #<TestClass> which has only a getter
console.log(t.s); //outputs "5"
console.log(t.p()) // outputs "7"

Keywords

FAQs

Package last updated on 21 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc