# JS-guide **Repository Path**: mirrors_photopea/JS-guide ## Basic Information - **Project Name**: JS-guide - **Description**: Our style of writing Javascript code - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-23 - **Last Updated**: 2026-03-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Javascript Guide Photopea is a set of libraries and other tools, written in Javascript. This guide shows our style of writing code, and mentions various related web technologies. It can be useful for writing any high-performance Javascript programs. We expect, that you already understand programming and at least one programming language. It is necessary to know the syntax of Javascript. You can learn it e.g. at [w3schools.com/js](https://www.w3schools.com/js/). ## Code style We try to avoid syntactic sugar. We always follow these rules when writing code: - use `var` instead of combining `var` / `let` / `const` - use `"hi"` instead of `'hi'` (quotation marks for strings) - use `==` instead of `===` (compare only values with the same data type) - use `null` instead of `undefined` - use `obj["prop"]==null` to check, if an object has a specific property - omit curly brackets if possible: `if(x) run();` instead of `if(x) { run(); }` ## Static functions Many of our code consists only of "static functions". These functions get all necessary data on the input. There is no `this`, `new`, `prototype` in such code. Functions are joined together by being defined as properties of one global object. All our public libraries (UPNG.js, UTIF.js, Typr.js ...) have such form. ```js var Point2D = {}; Point2D.length = function(p) { return Math.sqrt(p.x*p.x + p.y*p.y); } var Point3D = {}; Point3D.length = function(p) { var len = Point2D.length(p); return Math.sqrt(len*len + p.z*p.z); } ``` ```js var A = {x:1,y:1}, B = {x:1,y:1,z:1}; console.log(Point2D.length(A), Point3D.length(B)); ``` Such code is easy to rewrite into C (omit the global object `var ABC = {};`, change function names from `ABC.xyz` to `ABC_xyz`). Objects can be `struct`s. They never have methods. ## Prototypes You should be familiar with prototype-based programming, at least to a degree to understand the following code: ```js function Point2D(x, y) { this.x = x; this.y = y; } Point2D.prototype.length = function() { return Math.sqrt(this.x*this.x + this.y*this.y); } ``` ```js function Point3D(x, y, z) { Point2D.call(this, x, y); this.z = z; } Point3D.prototype = new Point2D(); Point3D.prototype.length = function() { var len = Point2D.prototype.length.call(this); return Math.sqrt(len*len + this.z*this.z); } ``` ```js var A = new Point2D(1,1), B = new Point3D(1,1,1); console.log(A.length(), B.length()); ``` ## Anonymous Functions Let's say we want to have an array A of all primes between 0 and 1000. We can use a following code. ```js var A = []; for(var i=2; i<1000; i++) { var prime=true; for(var j=2; j