---
# System prepended metadata

title: '[JavaScript] 關於重名'
tags: [JavaScript]

---

# [JavaScript] 關於重名
###### tags: `JavaScript`

### 若函數重名...
```=javascript
function fn(){
    console.log("fn");
}
function fn(a){
    console.log("a="+a);
}

fn(); //a=undefined

//交換順序
function fn(a){
    console.log("a="+a);
}
function fn(){
    console.log("fn");
}

fn(); //fn
```
將優先選擇最後載入的函數

### 若函數與變數重名...
- 變數無賦值
```=javascript
console.log(a); //function a() {}
var a;
function a() {}
console.log(a); //function a() {}
```
將優先選擇函數

- 變數有賦值
```=javascript
console.log(a); //function a() {}
var a=1;
function a() {}
console.log(a); //1
```
將優先選擇變數



---
[js中關於同名的情況(同名變量，同名函數，同名形參)](https://www.jianshu.com/p/92da7ed66221)