mirror of
https://github.com/airbnb/javascript.git
synced 2026-04-25 03:00:19 -04:00
[guide] [es5] Update whitespace and missing function names.
Per https://github.com/airbnb/javascript/issues/55#issuecomment-214672292
This commit is contained in:
@@ -267,7 +267,7 @@
|
||||
|
||||
```javascript
|
||||
// anonymous function expression
|
||||
var anonymous = function() {
|
||||
var anonymous = function () {
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -277,9 +277,9 @@
|
||||
};
|
||||
|
||||
// immediately-invoked function expression (IIFE)
|
||||
(function() {
|
||||
(function () {
|
||||
console.log('Welcome to the Internet. Please follow me.');
|
||||
})();
|
||||
}());
|
||||
```
|
||||
|
||||
- Never declare a function in a non-function block (if, while, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears.
|
||||
@@ -417,7 +417,7 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
function() {
|
||||
function () {
|
||||
test();
|
||||
console.log('doing stuff..');
|
||||
|
||||
@@ -433,7 +433,7 @@
|
||||
}
|
||||
|
||||
// good
|
||||
function() {
|
||||
function () {
|
||||
var name = getName();
|
||||
|
||||
test();
|
||||
@@ -449,7 +449,7 @@
|
||||
}
|
||||
|
||||
// bad - unnecessary function call
|
||||
function() {
|
||||
function () {
|
||||
var name = getName();
|
||||
|
||||
if (!arguments.length) {
|
||||
@@ -462,7 +462,7 @@
|
||||
}
|
||||
|
||||
// good
|
||||
function() {
|
||||
function () {
|
||||
var name;
|
||||
|
||||
if (!arguments.length) {
|
||||
@@ -517,7 +517,7 @@
|
||||
|
||||
anonymous(); // => TypeError anonymous is not a function
|
||||
|
||||
var anonymous = function() {
|
||||
var anonymous = function () {
|
||||
console.log('anonymous function expression');
|
||||
};
|
||||
}
|
||||
@@ -635,10 +635,10 @@
|
||||
}
|
||||
|
||||
// bad
|
||||
function() { return false; }
|
||||
function () { return false; }
|
||||
|
||||
// good
|
||||
function() {
|
||||
function () {
|
||||
return false;
|
||||
}
|
||||
```
|
||||
@@ -768,17 +768,17 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
function() {
|
||||
function () {
|
||||
∙∙∙∙var name;
|
||||
}
|
||||
|
||||
// bad
|
||||
function() {
|
||||
function () {
|
||||
∙var name;
|
||||
}
|
||||
|
||||
// good
|
||||
function() {
|
||||
function () {
|
||||
∙∙var name;
|
||||
}
|
||||
```
|
||||
@@ -847,14 +847,14 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
(function(global) {
|
||||
(function (global) {
|
||||
// ...stuff...
|
||||
})(this);
|
||||
```
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
(function(global) {
|
||||
(function (global) {
|
||||
// ...stuff...
|
||||
})(this);↵
|
||||
↵
|
||||
@@ -862,7 +862,7 @@
|
||||
|
||||
```javascript
|
||||
// good
|
||||
(function(global) {
|
||||
(function (global) {
|
||||
// ...stuff...
|
||||
})(this);↵
|
||||
```
|
||||
@@ -925,19 +925,19 @@
|
||||
|
||||
// bad
|
||||
var obj = {
|
||||
foo: function() {
|
||||
foo: function () {
|
||||
},
|
||||
bar: function() {
|
||||
bar: function () {
|
||||
}
|
||||
};
|
||||
return obj;
|
||||
|
||||
// good
|
||||
var obj = {
|
||||
foo: function() {
|
||||
foo: function () {
|
||||
},
|
||||
|
||||
bar: function() {
|
||||
bar: function () {
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1020,19 +1020,19 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
(function() {
|
||||
(function () {
|
||||
var name = 'Skywalker'
|
||||
return name
|
||||
})()
|
||||
|
||||
// good
|
||||
(function() {
|
||||
(function () {
|
||||
var name = 'Skywalker';
|
||||
return name;
|
||||
})();
|
||||
|
||||
// good (guards against the function becoming an argument when two files with IIFEs are concatenated)
|
||||
;(function() {
|
||||
;(function () {
|
||||
var name = 'Skywalker';
|
||||
return name;
|
||||
})();
|
||||
@@ -1193,25 +1193,25 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
function() {
|
||||
function () {
|
||||
var self = this;
|
||||
return function() {
|
||||
return function () {
|
||||
console.log(self);
|
||||
};
|
||||
}
|
||||
|
||||
// bad
|
||||
function() {
|
||||
function () {
|
||||
var that = this;
|
||||
return function() {
|
||||
return function () {
|
||||
console.log(that);
|
||||
};
|
||||
}
|
||||
|
||||
// good
|
||||
function() {
|
||||
function () {
|
||||
var _this = this;
|
||||
return function() {
|
||||
return function () {
|
||||
console.log(_this);
|
||||
};
|
||||
}
|
||||
@@ -1221,7 +1221,7 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
var log = function(msg) {
|
||||
var log = function (msg) {
|
||||
console.log(msg);
|
||||
};
|
||||
|
||||
@@ -1297,11 +1297,11 @@
|
||||
this.set('lightsaber', lightsaber);
|
||||
}
|
||||
|
||||
Jedi.prototype.set = function(key, val) {
|
||||
Jedi.prototype.set = function set(key, val) {
|
||||
this[key] = val;
|
||||
};
|
||||
|
||||
Jedi.prototype.get = function(key) {
|
||||
Jedi.prototype.get = function get(key) {
|
||||
return this[key];
|
||||
};
|
||||
```
|
||||
@@ -1343,12 +1343,12 @@
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
Jedi.prototype.jump = function() {
|
||||
Jedi.prototype.jump = function jump() {
|
||||
this.jumping = true;
|
||||
return true;
|
||||
};
|
||||
|
||||
Jedi.prototype.setHeight = function(height) {
|
||||
Jedi.prototype.setHeight = function setHeight(height) {
|
||||
this.height = height;
|
||||
};
|
||||
|
||||
@@ -1357,12 +1357,12 @@
|
||||
luke.setHeight(20); // => undefined
|
||||
|
||||
// good
|
||||
Jedi.prototype.jump = function() {
|
||||
Jedi.prototype.jump = function jump() {
|
||||
this.jumping = true;
|
||||
return this;
|
||||
};
|
||||
|
||||
Jedi.prototype.setHeight = function(height) {
|
||||
Jedi.prototype.setHeight = function setHeight(height) {
|
||||
this.height = height;
|
||||
return this;
|
||||
};
|
||||
@@ -1404,7 +1404,7 @@
|
||||
|
||||
...
|
||||
|
||||
$(this).on('listingUpdated', function(e, listingId) {
|
||||
$(this).on('listingUpdated', function (e, listingId) {
|
||||
// do something with listingId
|
||||
});
|
||||
```
|
||||
@@ -1417,7 +1417,7 @@
|
||||
|
||||
...
|
||||
|
||||
$(this).on('listingUpdated', function(e, data) {
|
||||
$(this).on('listingUpdated', function (e, data) {
|
||||
// do something with data.listingId
|
||||
});
|
||||
```
|
||||
@@ -1435,7 +1435,7 @@
|
||||
```javascript
|
||||
// fancyInput/fancyInput.js
|
||||
|
||||
!function(global) {
|
||||
!function (global) {
|
||||
'use strict';
|
||||
|
||||
var previousFancyInput = global.FancyInput;
|
||||
@@ -1530,7 +1530,7 @@
|
||||
- **Yup.**
|
||||
|
||||
```javascript
|
||||
function() {
|
||||
function () {
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user