|
| 1 | +## `ES5` |
| 2 | + |
| 3 | +The ES5 option converts most ES6+ features into ES5 compatible code. |
| 4 | + |
| 5 | +Option name: `es5` |
| 6 | + |
| 7 | +Option values: `true/false` |
| 8 | + |
| 9 | +Note: Does not cover all cases such as Promises or Generator functions. Use [Babel](https://babel.dev/). |
| 10 | + |
| 11 | +The ES5 option is intended to undo any ES6 feature the obfuscator adds to your code. If you input ES5 code, and enable the `es5` option, you can be guaranteed to have ES5 compatible output. |
| 12 | + |
| 13 | +## Example |
| 14 | + |
| 15 | +```js |
| 16 | +// Input |
| 17 | +function print(...messages){ |
| 18 | + console.log(...messages); // The spread operator (...) |
| 19 | + // was introduced in ES6! |
| 20 | +} |
| 21 | + |
| 22 | +print("Hello", "World"); // "Hello World" |
| 23 | + |
| 24 | +// Output |
| 25 | +var __p_2580918143; |
| 26 | +function print() { |
| 27 | + var __p_7607361496; |
| 28 | + var messages, __p_2591841272 = (__p_7607361496 = Array.prototype.slice.call(arguments), messages = __p_7607361496.slice(0)); |
| 29 | + (__p_2580918143 = console).log.apply(__p_2580918143, [].concat(Array.prototype.slice.call(messages))); |
| 30 | +} |
| 31 | +print('Hello', 'World'); // "Hello World" |
| 32 | +``` |
| 33 | + |
| 34 | +## Polyfill Array Methods |
| 35 | + |
| 36 | +When the ES5 option is enabled, array method polyfills will be injected to the top of your script. |
| 37 | + |
| 38 | +```js |
| 39 | +if (!Array.prototype.forEach) { |
| 40 | + Array.prototype.forEach = function forEach(callback, thisArg) { |
| 41 | + if (typeof callback !== 'function') { |
| 42 | + throw new TypeError(callback + ' is not a function'); |
| 43 | + } |
| 44 | + var array = this; |
| 45 | + thisArg = thisArg || this; |
| 46 | + for (var i = 0, l = array.length; i !== l; ++i) { |
| 47 | + callback.call(thisArg, array[i], i, array); |
| 48 | + } |
| 49 | + }; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Destructuring |
| 54 | + |
| 55 | +The ES5 option supports transpiling the destructuring patterns. |
| 56 | + |
| 57 | +```js |
| 58 | +// Input |
| 59 | +var {userName, email} = { userName: "John", email: "email@exampe.com" }; |
| 60 | + |
| 61 | +// Output |
| 62 | +var __p_7467473759; |
| 63 | +var userName, email, __p_4755992742 = (__p_7467473759 = { |
| 64 | + userName: 'John', |
| 65 | + email: 'email@exampe.com' |
| 66 | + }, userName = __p_7467473759.userName, email = __p_7467473759.email); |
| 67 | +``` |
| 68 | + |
| 69 | +## Spread Operator |
| 70 | + |
| 71 | +The ES5 option supports transpiling the spread operator. |
| 72 | + |
| 73 | +```js |
| 74 | +// Input |
| 75 | +array.push(...objects); |
| 76 | + |
| 77 | +// Output |
| 78 | +var __p_6344935930; |
| 79 | +(__p_6344935930 = array).push.apply(__p_6344935930, [].concat(Array.prototype.slice.call(objects))); |
| 80 | +``` |
| 81 | + |
| 82 | +## Template String |
| 83 | + |
| 84 | +The ES5 option supports transpiling template strings. |
| 85 | + |
| 86 | +```js |
| 87 | +// Input |
| 88 | +var myString = `Hello ${userName}`; |
| 89 | + |
| 90 | +// Output |
| 91 | +var myString = 'Hello ' + (userName + ''); |
| 92 | +``` |
| 93 | + |
| 94 | +## Object getters/setters |
| 95 | + |
| 96 | +The ES5 option supports transpiling getter and setter methods. |
| 97 | + |
| 98 | +```js |
| 99 | +// Input |
| 100 | +var _name; |
| 101 | +var myObject = { |
| 102 | + get name(){ |
| 103 | + return _name; |
| 104 | + }, |
| 105 | + set name(newName){ |
| 106 | + _name = newName; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +// Output |
| 111 | +function __p_6886881506(base, computedProps, getters, setters) { |
| 112 | + for (var i = 0; i < computedProps.length; i++) { |
| 113 | + base[computedProps[i][0]] = computedProps[i][1]; |
| 114 | + } |
| 115 | + var keys = Object.create(null); |
| 116 | + Object.keys(getters).forEach(function (key) { |
| 117 | + return keys[key] = 1; |
| 118 | + }); |
| 119 | + Object.keys(setters).forEach(function (key) { |
| 120 | + return keys[key] = 1; |
| 121 | + }); |
| 122 | + Object.keys(keys).forEach(function (key) { |
| 123 | + Object.defineProperty(base, key, { |
| 124 | + set: setters[key], |
| 125 | + get: getters[key], |
| 126 | + configurable: true |
| 127 | + }); |
| 128 | + }); |
| 129 | + return base; |
| 130 | +} |
| 131 | +var _name; |
| 132 | +var myObject = __p_6886881506({}, [], { |
| 133 | + 'name': function () { |
| 134 | + return _name; |
| 135 | + } |
| 136 | +}, { |
| 137 | + 'name': function (newName) { |
| 138 | + _name = newName; |
| 139 | + } |
| 140 | +}); |
| 141 | +``` |
| 142 | + |
| 143 | +## Arrow Functions |
| 144 | + |
| 145 | +The ES5 option converts arrow functions into regular functions. |
| 146 | + |
| 147 | +```js |
| 148 | +// Input |
| 149 | +var print = message => console.log(message); |
| 150 | + |
| 151 | +// Output |
| 152 | +var print = function (message) { |
| 153 | + return console.log(message); |
| 154 | +}; |
| 155 | +``` |
| 156 | + |
| 157 | +## Const/Let |
| 158 | + |
| 159 | +The ES5 option converts `const` and `let` to a regular `var` keyword. |
| 160 | + |
| 161 | +```js |
| 162 | +// Input |
| 163 | +let myVar1 = true; |
| 164 | +const myVar2 = "String"; |
| 165 | + |
| 166 | +// Output |
| 167 | +var myVar1 = true; |
| 168 | +var myVar2 = 'String'; |
| 169 | +``` |
| 170 | + |
| 171 | +## Classes |
| 172 | + |
| 173 | +The ES5 option partially supports transpiling classes. |
| 174 | + |
| 175 | +## Reserved Identifiers |
| 176 | + |
| 177 | +The ES5 option will change any illegal uses of reserved identifiers. |
| 178 | + |
| 179 | +```js |
| 180 | +// Input |
| 181 | +var myObject = {true: 1}; |
| 182 | +myObject.for = true; |
| 183 | + |
| 184 | +// Output |
| 185 | +var myObject = {"true": 1}; |
| 186 | +myObject["for"] = true; |
| 187 | +``` |
| 188 | + |
| 189 | +## Features not supported |
| 190 | + |
| 191 | +- Promises |
| 192 | +- Async / Await |
| 193 | +- Generator functions |
| 194 | +- Nullish coalescing |
| 195 | +- Optional chaining |
| 196 | + |
| 197 | +Use [Babel](https://babel.dev/) to transpile these features. JS-Confuser will only support features the obfuscator may potentially add to your code. |
0 commit comments