[gnome-shell/wip/fmuellner/doc-updates] Update HACKING.md
- From: Florian Müllner <fmuellner src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-shell/wip/fmuellner/doc-updates] Update HACKING.md
- Date: Wed, 23 May 2018 16:40:32 +0000 (UTC)
commit 9d98ba6693f9bc7ef0c59fe3839636a935b520a0
Author: Florian Müllner <florian muellner gmail com>
Date: Wed May 23 16:39:54 2018 +0000
Update HACKING.md
HACKING.md | 41 ++++++++++++++++++++++++++---------------
1 file changed, 26 insertions(+), 15 deletions(-)
---
diff --git a/HACKING.md b/HACKING.md
index 702977b7d..8c064999a 100644
--- a/HACKING.md
+++ b/HACKING.md
@@ -19,7 +19,7 @@ on one line.
* One space after the `function` keyword. No space between the function name
* in a declaration or a call. One space before the parens in the `if`
* statements, or `while`, or `for` loops.
-
+```javascript
function foo(a, b) {
let bar;
@@ -36,6 +36,7 @@ on one line.
print(20);
}
}
+```
## Semicolons
@@ -65,9 +66,9 @@ library. These headers are not installed, distributed or introspected.
Use UpperCamelCase when importing modules to distinguish them from ordinary
variables, e.g.
-
+```javascript
const GLib = imports.gi.GLib;
-
+```
Imports should be categorized into one of two places. The top-most import block
should contain only "environment imports". These are either modules from
gobject-introspection or modules added by gjs itself.
@@ -78,7 +79,7 @@ e.g. `imports.ui.popupMenu`.
Each import block should be sorted alphabetically. Don't import modules you
don't use.
-
+```javascript
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
@@ -88,7 +89,7 @@ don't use.
const Params = imports.misc.params;
const Tweener = imports.ui.tweener;
const Util = imports.misc.util;
-
+```
The alphabetical ordering should be done independently of the location of the
location. Never reference `imports` in actual code.
@@ -96,13 +97,14 @@ location. Never reference `imports` in actual code.
We use CONSTANTS_CASE to define constants. All constants should be directly
under the imports:
-
+```javascript
const MY_DBUS_INTERFACE = 'org.my.Interface';
+```
## Variable declaration
Always use either `const` or `let` when defining a variable.
-
+```javascript
// Iterating over an array
for (let i = 0; i < arr.length; ++i) {
let item = arr[i];
@@ -112,6 +114,7 @@ Always use either `const` or `let` when defining a variable.
for (let prop in someobj) {
...
}
+```
If you use "var" then the variable is added to function scope, not block scope.
See [What's new in JavaScript
1.7](https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.7#Block_scope_with_let_%28Merge_into_let_Statement%29)
@@ -121,7 +124,7 @@ See [What's new in JavaScript 1.7](https://developer.mozilla.org/en/JavaScript/N
There are many approaches to classes in JavaScript. We use our own class framework
(sigh), which is built in gjs. The advantage is that it supports inheriting from
GObjects, although this feature isn't used very often in the Shell itself.
-
+```javascript
var IconLabelMenuItem = new Lang.Class({
Name: 'IconLabelMenuItem',
Extends: PopupMenu.PopupMenuBaseItem,
@@ -136,6 +139,7 @@ GObjects, although this feature isn't used very often in the Shell itself.
log("menu opened!");
}
});
+```
* 'Name' is required. 'Extends' is optional. If you leave it out, you will
automatically inherit from Object.
@@ -157,7 +161,7 @@ GObjects, although this feature isn't used very often in the Shell itself.
GObject Introspection is a powerful feature that allows us to have native
bindings for almost any library built around GObject. If a library requires
you to inherit from a type to use it, you can do so:
-
+```javascript
var MyClutterActor = new Lang.Class({
Name: 'MyClutterActor',
Extends: Clutter.Actor,
@@ -177,6 +181,7 @@ you to inherit from a type to use it, you can do so:
alloc.x2, alloc.y2);
}
});
+```
## Translatable strings, `environment.js`
@@ -201,7 +206,7 @@ that has a property called `actor`. We call this wrapper class the "delegate".
We sometimes use expando properties to set a property called `_delegate` on
the actor itself:
-
+```javascript
var MyClass = new Lang.Class({
Name: 'MyClass',
@@ -216,6 +221,7 @@ the actor itself:
actor.set_label("You clicked the button!");
}
});
+```
The 'delegate' property is important for anything which trying to get the
delegate object from an associated actor. For instance, the drag and drop
@@ -239,15 +245,16 @@ variable that can be captured in closures.
All closures should be wrapped with Function.prototype.bind or use arrow
notation.
-
+```javascript
const Lang = imports.lang;
let closure1 = () => { this._fnorbate(); };
let closure2 = this._fnorbate.bind(this);
+```
A more realistic example would be connecting to a signal on a method of a
prototype:
-
+```javascript
const Lang = imports.lang;
const FnorbLib = imports.fborbLib;
@@ -261,18 +268,21 @@ prototype:
this._updateFnorb();
}
});
+```
## Object literal syntax
In JavaScript, these are equivalent:
-
+```javascript
foo = { 'bar': 42 };
foo = { bar: 42 };
+```
and so are these:
-
+```javascript
var b = foo['bar'];
var b = foo.bar;
+```
If your usage of an object is like an object, then you're defining "member
variables." For member variables, use the no-quotes no-brackets syntax: `{ bar:
@@ -288,7 +298,7 @@ Getters and setters should be used when you are dealing with an API that is
designed around setting properties, like Tweener. If you want to animate an
arbitrary property, create a getter and setter, and use Tweener to animate the
property.
-
+```javascript
var ANIMATION_TIME = 2000;
var MyClass = new Lang.Class({
@@ -314,3 +324,4 @@ property.
{ position: 100,
time: ANIMATION_TIME,
transition: 'easeOutQuad' });
+```
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]