---
title: "JavaScript的变量型函数和定义型函数"
author: "yoyo08"
date: "2013-06-03T14:56:37Z"
tags: []
categories: ["Web前端"]
slug: "javascript"
draft: false
lastmod: "2013-06-03T14:56:37Z"
id: 1881563
url: "https://yoyo08.iteye.com/blog/1881563"
---

JavaScript的变量型函数和定义型函数

 

1\. JavaScript的函数类型

变量型函数

```
var test = function(){
 
}
```

 

定义型函数

```
function test(){
 
}
```

 

2\. 测试代码1

```
<script>

var test = function(){
  alert("first");
}
test();

test = function(){
  alert("second");
}
test();

</script>
```

 

 输出结果为：

  first  
  second

 

3\. 测试代码2

```
<script>

function test(){
 alert("first");
}
test();

function test(){
  alert("second");
}
test();

</script>
```

 

 输出结果为：

  second  
  second

 

4\. 测试代码3

```
<script>

function test(){
 alert("first");
}
test();

</script>

<script>

function test(){
  alert("second");
}
test();

</script>
```

 

 输出结果为：

  first  
  second

 

5\. 说明

 Javascirpt执行引擎并非一行一行地分析和执行程序，而是一段一段地分析执行。在同一段程序的分析执行中，定义式的函数语句会被提取出来优先执行。函数定义执行完之后，才会按顺序执行其他语句代码。