{"id":13659930,"url":"https://github.com/TremayneChrist/ProtectJS","last_synced_at":"2025-04-24T15:31:22.648Z","repository":{"id":12998885,"uuid":"15678024","full_name":"TremayneChrist/ProtectJS","owner":"TremayneChrist","description":"Private methods \u0026 properties in JavaScript","archived":true,"fork":false,"pushed_at":"2017-10-15T15:13:47.000Z","size":248,"stargazers_count":94,"open_issues_count":8,"forks_count":5,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-11T08:25:12.705Z","etag":null,"topics":["javascript","lock","private","private-js","private-members","private-method","protection"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TremayneChrist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-06T15:52:24.000Z","updated_at":"2023-05-04T18:08:36.000Z","dependencies_parsed_at":"2022-09-13T15:02:34.708Z","dependency_job_id":null,"html_url":"https://github.com/TremayneChrist/ProtectJS","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TremayneChrist%2FProtectJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TremayneChrist%2FProtectJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TremayneChrist%2FProtectJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TremayneChrist%2FProtectJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TremayneChrist","download_url":"https://codeload.github.com/TremayneChrist/ProtectJS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223958146,"owners_count":17231746,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","lock","private","private-js","private-members","private-method","protection"],"created_at":"2024-08-02T05:01:13.953Z","updated_at":"2024-11-10T13:31:32.055Z","avatar_url":"https://github.com/TremayneChrist.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# ProtectJS / Protect JS\n\n[![Build Status](https://travis-ci.org/TremayneChrist/ProtectJS.svg?branch=master)](https://travis-ci.org/TremayneChrist/ProtectJS)\n[![Coverage Status](https://coveralls.io/repos/github/TremayneChrist/ProtectJS/badge.svg?branch=master)](https://coveralls.io/github/TremayneChrist/ProtectJS?branch=master)\n\n#### Methods/Functions\nAdding private methods to an object in JavaScript has always been an awkward thing to do, as JavaScript doesn't exactly support it. Instead, we either place enclosed functions in the constructor, which consumes more memory per object, or, we enclose both the object definition and our private methods inside of a closure.\n\nThe latter is a good way to accomplish private methods in JavaScript as it creates truly private methods and doesn't add to the memory consumption. The problem with it is, is that your private methods are separate from the object and your coding style is different compared to public definitions on the prototype.\n\n**ProtectJS** is a library that extends objects you define, to allow you to add all your private methods to the prototype chain. It does this by adding protection checks to all of your methods, any marked with a prepending underscore (_) is treated as a private method.\n\n#### Other properties\n\nAs well as methods, JavaScript doesn't exactly support any type of private property. This becomes a problem when dangerous modifications to these properties occur.\n\nThink of complex algorithms - If these could be modified by external sources, their output would be completely invalidated, potentially causing huge implications.\n\n\n### Examples\n\nIn many languages, it is custom to prepend private variables with an underscore (_), so we use this in JavaScript to show that an object should be considered as a private. Obviously this has absolutely no affect to whether it is actually private or not, it's more of a visual reference for a developer.\n\nLet's create a basic object using this approach...\n\n```javascript\n\n// Create the object\nfunction MyObject() {}\n\n// This is our public method\nMyObject.prototype.public = function () {\n  console.log('PUBLIC method has been called');\n};\n\n// This is our private method, using (_)\nMyObject.prototype._private = function () {\n  console.log('PRIVATE method has been called');\n};\n\n// Create an instance of the object\nvar mo = new MyObject();\n\n// Call its methods\nmo.public(); // Pass\nmo._private(); // Pass\n\n```\nAs I stated previously, there is no protection here, so I could easily call the **_private()** method and get a message saying \"PRIVATE method has been called\".\n\n**Now let's protect the object with ProtectJS!..**\n\nAdd ProtectJS into your source, making sure it loads before the code you want to protect.\n\n```html\n\u003chead\u003e\n    \u003ctitle\u003ePrivateJS\u003c/title\u003e\n    \u003cscript type=\"text/javascript\" src=\"ProtectJS/protect.min.js\"\u003e\u003c/script\u003e\n    \u003cscript type=\"text/javascript\" src=\"app.js\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n```\n\nNow that ProtectJS is available, we need to use it on the object\n\n```javascript\n\n// Create the object\nfunction MyObject() {}\n\n// This is our public method\nMyObject.prototype.public = function () {\n  console.log('PUBLIC method has been called');\n};\n\n// This is our private method, using (_)\nMyObject.prototype._private = function () {\n  console.log('PRIVATE method has been called');\n};\n\nprotect(MyObject); // Protect the object prototype\n\n// Create an instance of the object\nvar mo = new MyObject();\n\n// Call its methods\nmo.public(); // Pass\nmo._private(); // Fail\n\n```\n\nProtectJS assumes that all methods starting with underscores in the prototype are private, and will add protection checks to prevent them from being called outside of the object.\n\nOnce protected, the only way to call a private method is by calling it through another method inside the same object.\n\n\n---\n\n### Pros\n\n1. Enables private methods to be added onto the prototype, keeping object memory to a minimum.\n\n2. Protects your other properties from being modified by external sources.\n\n3. Keeps code creation clean and easy to read.\n\n4. Allows you to define objects in a natural way.\n\n5. Doesn't change your coding style.\n\n6. Protects properties you don't want to be used.\n\n\n### Cons\n\n1. You have to include the ProtectJS library in your project.\n2. Marginal overheads to be considered (Performance tests to come)\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTremayneChrist%2FProtectJS","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTremayneChrist%2FProtectJS","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTremayneChrist%2FProtectJS/lists"}