Skip to content

Commit 6cbabd4

Browse files
Merge pull request #12343 from nextcloud/fix/improve-js-codeexamples
fix(developer): Modernize JavaScript codestyle examples
2 parents 6df1e4b + ffeac9c commit 6cbabd4

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

developer_manual/getting_started/codingguidelines.rst

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ General
2020
Labels
2121
------
2222

23-
We assign labels to issues and pull requests to make it easy to find them and to signal what needs to be done. Some of these are assigned by the developers, others by QA, bug triagers, project lead or maintainers and so on. It is not desired that users/reporters of bugs assign labels themselves, unless they are developers/contributors to Nextcloud.
23+
We assign labels to issues and pull requests to make it easy to find them and to signal what needs to be done.
24+
Some of these are assigned by the developers, others by QA, bug triagers, project lead or maintainers and so on.
25+
It is not desired that users/reporters of bugs assign labels themselves, unless they are developers/contributors to Nextcloud.
2426

2527
The most important labels and their meaning:
2628

@@ -31,7 +33,8 @@ The most important labels and their meaning:
3133
* ``2. developing`` - development in progress
3234
* ``3. to review`` - ready for review
3335
* ``4. to release`` - reviewed PR that awaits unfreeze of a branch to get merged or has pending CI jobs
34-
* ``needs info`` - this issue needs further information from the reporter, see :doc:`../../prologue/bugtracker/triaging`. This tag is typically combined with ``0. to triage`` to signal a bug report is not confirmed yet or a feature request has not been approved.
36+
* ``needs info`` - this issue needs further information from the reporter, see :doc:`../../prologue/bugtracker/triaging`.
37+
This tag is typically combined with ``0. to triage`` to signal a bug report is not confirmed yet or a feature request has not been approved.
3538

3639
* Tags showing the type of issue or PR
3740

@@ -271,15 +274,16 @@ with ``Data``.
271274
}
272275
273276
274-
JavaScript
275-
----------
277+
JavaScript and Typescript
278+
-------------------------
276279
277-
There is a shared configuration for `eslint <https://eslint.org/>`_ that you can use to automatically format your Nextcloud apps's JavaScript code. It consists of two parts: a `config package <https://github.com/nextcloud/eslint-config>`_ that contains the formatting preferences and a `plugin <https://github.com/nextcloud/eslint-plugin>`_ to detect deprecated and removed APIs in your code. See their readmes for instructions.
280+
There is a shared configuration for `eslint <https://eslint.org/>`_ that you can use to automatically format your Nextcloud apps's JavaScript code.
281+
It consists of two parts: a `config package <https://github.com/nextcloud-libraries/eslint-config>`_ that contains the formatting preferences and a `plugin <https://github.com/nextcloud-libraries/eslint-plugin>`_ to detect deprecated and removed APIs in your code. See their readmes for instructions.
278282
279-
* Use a :file:`js/main.js` or :file:`js/app.js` where your program is started
283+
* Use a :file:`js/main.js` or :file:`js/app.ts` where your program is started
280284
* Use **const** or **let** to limit variable to local scope
281-
* Use JavaScript strict mode
282-
* Use a global namespace object where you bind publicly used functions and objects to
285+
* Use JavaScript strict mode (automatically the case when using JavaScript modules)
286+
* Use a global namespace object where you bind publicly used functions and objects to instead of plain global variables.
283287
284288
**DO**:
285289
@@ -321,36 +325,37 @@ This is how you'd do inheritance in JavaScript:
321325
322326
.. code-block:: javascript
323327
324-
// create parent object and bind methods to it
325-
var ParentObject = function(name) {
326-
this.name = name;
327-
};
328-
329-
ParentObject.prototype.sayHello = function() {
330-
console.log(this.name);
331-
}
328+
class ParentClass {
329+
// a public property
330+
name;
331+
// names prefixed with # are private in JavaScript and can not be accessed from outside
332+
// #privateProperty;
332333
334+
constructor(name) {
335+
this.name = name;
336+
}
333337
334-
// create childobject, call parents constructor and inherit methods
335-
var ChildObject = function(name, age) {
336-
ParentObject.call(this, name);
337-
this.age = age;
338-
};
338+
sayHello() {
339+
console.log(this.name);
340+
}
341+
}
339342
340-
ChildObject.prototype = Object.create(ParentObject.prototype);
343+
class ChildClass extends ParentClass {
344+
age;
341345
342-
// overwrite parent method
343-
ChildObject.prototype.sayHello = function() {
344-
// call parent method if you want to
345-
ParentObject.prototype.sayHello.call(this);
346-
console.log('childobject');
347-
};
346+
constructor(name, age) {
347+
super(name);
348+
this.age = age;
349+
}
348350
349-
var child = new ChildObject('toni', 23);
351+
// overwrite parent method
352+
sayHello() {
353+
console.log('child class: ', this.name, this.age);
354+
}
355+
}
350356
351-
// prints:
352-
// toni
353-
// childobject
357+
const child = new ChildClass('toni', 23)
358+
// Prints: child class: toni 23
354359
child.sayHello();
355360
356361
Objects, functions & variables
@@ -360,18 +365,18 @@ Use *UpperCamelCase* for Objects, *lowerCamelCase* for functions and variables.
360365
361366
.. code-block:: javascript
362367
363-
var MyObject = function() {
368+
const MyObject = function() {
364369
this.attr = "hi";
365370
};
366371
367-
var myFunction = function() {
372+
const myFunction = function() {
368373
return true;
369374
};
370375
371-
var myVariable = 'blue';
376+
const myVariable = 'blue';
372377
373-
var objectLiteral = {
374-
value1: 'somevalue'
378+
const objectLiteral = {
379+
value1: 'somevalue',
375380
};
376381
377382
@@ -424,15 +429,15 @@ Control structures
424429
}
425430
426431
// for loop
427-
for (var i = 0; i < 4; i++) {
432+
for (let i = 0; i < 4; i++) {
428433
// your code
429434
}
430435
431436
// switch
432437
switch (value) {
433438
434439
case 'hi':
435-
// yourcode
440+
// your code
436441
break;
437442
438443
default:

0 commit comments

Comments
 (0)