###### tags: `jptw` `thesis` `technology` `html/css/js` `webpage`
# Notes for JavaScript
## If-else
```javascript
a && (//do something);
b || (//do something);
a ? ( (b = 1), (c = 1) ) : ( (b = 2), (c = 2) ) );
```
> Ref: [[JavaScript] 判斷式 if…else 的各種寫法](http://skyroxas.tw/javascript-%E5%88%A4%E6%96%B7%E5%BC%8F-else-%E7%9A%84%E5%90%84%E7%A8%AE%E5%AF%AB%E6%B3%95/)
## ForEach
```javascript
// for loop
for( let i = 0 ; i < arr.length ; i++ ){
console.log(arr[i]);
}
// for each
arr.forEach(item => console.log(item))
```
> Ref: [Array 原型的 forEach 有多好用? 學會高階函數之後都不想寫 JavaScript 以外的程式語言了](https://realdennis.medium.com/array-%E5%8E%9F%E5%9E%8B%E7%9A%84-foreach-%E6%9C%89%E5%A4%9A%E5%A5%BD%E7%94%A8-%E5%AD%B8%E6%9C%83%E9%AB%98%E9%9A%8E%E5%87%BD%E6%95%B8%E4%B9%8B%E5%BE%8C%E9%83%BD%E4%B8%8D%E6%83%B3%E5%AF%AB-javascript-%E4%BB%A5%E5%A4%96%E7%9A%84%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80%E4%BA%86-dc4b594a045a)
## Define Method
```javascript
// on single object
var a = {}, or a = function() {}, or a = [], etc.
a.dotFunction = function() { return 'hi'; }
// on instances of class
function someClass() {
}
someClass.prototype.dotFunction = function() { return 'hi'; };
```
> Ref: [How to make a “dot function” in javascript](https://stackoverflow.com/questions/19872917/how-to-make-a-dot-function-in-javascript)
## Axios
**POST Request**
```javascript
var body = {
'UserName': 'Adam',
'userEmail': 'adam@example.com'
}
axios({
method: 'POST',
url: '/adduser',
data: body
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
```
If post a file, add the file to a `formData` object and set the header.
```javascript
var formData = new FormData();
var file = document.querySelector('#file');
formData.append('file', file.files[0]);
axios.post( 'url_for_upload_file', formData, {
headers: {
'Content-Type: 'multipart/form-data
}
})
```
> Ref:
> [1] [axios post request to send form data](https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data)
> [2] [How to post a file from a form with Axios](https://stackoverflow.com/questions/43013858/how-to-post-a-file-from-a-form-with-axios)
> [3] [Get data from file input in JQuery](https://stackoverflow.com/questions/12281775/get-data-from-file-input-in-jquery)
>