└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Javascript Guide 2 | 3 | Photopea is a set of libraries and other tools, written in Javascript. This guide shows our style of writing code, 4 | and mentions various related web technologies. It can be useful for writing any high-performance Javascript programs. 5 | 6 | We expect, that you already understand programming and at least one programming language. 7 | It is necessary to know the syntax of Javascript. You can learn it e.g. at [w3schools.com/js](https://www.w3schools.com/js/). 8 | 9 | ## Code style 10 | We try to avoid syntactic sugar. We always follow these rules when writing code: 11 | 12 | - use `var` instead of combining `var` / `let` / `const` 13 | - use `"hi"` instead of `'hi'` (quotation marks for strings) 14 | - use `==` instead of `===` (compare only values with the same data type) 15 | - use `null` instead of `undefined` 16 | - use `obj["prop"]==null` to check, if an object has a specific property 17 | - omit curly brackets if possible: `if(x) run();` instead of `if(x) { run(); }` 18 | 19 | ## Static functions 20 | 21 | Many of our code consists only of "static functions". These functions get all necessary data on the input. 22 | There is no `this`, `new`, `prototype` in such code. Functions are joined together by being defined as properties of one global object. 23 | All our public libraries (UPNG.js, UTIF.js, Typr.js ...) have such form. 24 | 25 | ```js 26 | var Point2D = {}; 27 | Point2D.length = function(p) { return Math.sqrt(p.x*p.x + p.y*p.y); } 28 | var Point3D = {}; 29 | Point3D.length = function(p) { var len = Point2D.length(p); return Math.sqrt(len*len + p.z*p.z); } 30 | ``` 31 | ```js 32 | var A = {x:1,y:1}, B = {x:1,y:1,z:1}; 33 | console.log(Point2D.length(A), Point3D.length(B)); 34 | ``` 35 | 36 | Such code is easy to rewrite into C (omit the global object `var ABC = {};`, change function names from `ABC.xyz` to `ABC_xyz`). 37 | Objects can be `struct`s. They never have methods. 38 | 39 | ## Prototypes 40 | 41 | You should be familiar with prototype-based programming, at least to a degree to understand the following code: 42 | 43 | ```js 44 | function Point2D(x, y) { 45 | this.x = x; 46 | this.y = y; 47 | } 48 | Point2D.prototype.length = function() { 49 | return Math.sqrt(this.x*this.x + this.y*this.y); 50 | } 51 | ``` 52 | 53 | ```js 54 | function Point3D(x, y, z) { 55 | Point2D.call(this, x, y); 56 | this.z = z; 57 | } 58 | Point3D.prototype = new Point2D(); 59 | Point3D.prototype.length = function() { 60 | var len = Point2D.prototype.length.call(this); 61 | return Math.sqrt(len*len + this.z*this.z); 62 | } 63 | ``` 64 | ```js 65 | var A = new Point2D(1,1), B = new Point3D(1,1,1); 66 | console.log(A.length(), B.length()); 67 | ``` 68 | 69 | ## Anonymous Functions 70 | 71 | Let's say we want to have an array A of all primes between 0 and 1000. We can use a following code. 72 | 73 | ```js 74 | var A = []; 75 | for(var i=2; i<1000; i++) { 76 | var prime=true; for(var j=2; j