if (1 > 0) {
     document.write('a');
     document.write('b');
     document.write('c');
   }
if (2 > 0) {
     document.write('a');
     document.write('b');
     document.write('c');
   }
if (3 > 0) {
     document.write('a');
     document.write('b');
     document.write('c');
   }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

此类写法容易产生代码偶合(代码重复) 编程的基本原则就是高内聚,弱偶合. 所以就延伸到了函数 function

function test() {
//function 声明 test 函数名
     document.write('a')
     document.write('b')
     document.write('c')
}
   test();      //abc
   test();      //abc   用几次调用几次
1
2
3
4
5
6
7
8

代码可以写在函数里面 例子

function test() {
     var a = 123;
     var b = 234;
     var c = a + b;
     document.write(c);
}
   test();      //执行一次c
   test();      //执行两次c
1
2
3
4
5
6
7
8
function test() { 
//test也是一个变量 后面加一个小括号 小括号后面加大括号 里面时代码体
     document.write('hello world!')
}
     test();        //hello world!
1
2
3
4
5

开发规范

函数命名规则采用小驼峰式命名规则 变量 theFirstName

小驼峰式命名: theFirstName 首字母小写 后面单词首字母大写

大驼峰式命名: TheFirstName 每个字母都要大写

一个单词的话怎么写都无所谓

# 两种函数

1.函数声明

function test (){
 document.write(a);
 }
1
2
3

2.函数表达式(命名函数表达式)

var test = function abc() {
     //这个函数的名字依然是test
     document.write('a');
   }
1
2
3
4

函数表达式(匿名函数表达式)

var demo = function () {
     document.write('b');
     //demo.name=demo
   }
1
2
3
4

通常用匿名函数

# 函数写法

function test(a, b) {
//ab相当于声明了两个变量
     document.write(a + b);
     //传参形式
   }
    test(1, 2);             //3
    //ab是形参不是一个数 只占个位置   1,2是实参
1
2
3
4
5
6
7

# 例子

function sum(a, b) {
     var c = a + b;
        document.write(c);
}
   sum(1, 2);       //输出3    x和y随便换值  可以实现算数 
   sum(3, 4);       //输出7
1
2
3
4
5
6

# 例子

输入两个数,如果 a 比 b 大输出两个数的差 如果 a 比 10 小,输出两个数的和 否则输出 10

function sum(a, b) {
     if (a > 10) {
       document.write(a - b);
     } else if (a < 10) {
       document.write(a + b);
     } else {
       document.write(10)
     }
}
      sum(11, 2) //输出9
1
2
3
4
5
6
7
8
9
10

传参规则

形参多实参少 形参abcd 实参1 2 3 打印出123undefined

实参比形参多 从左到右替换值

# 例子

function sum(a, b, c, d) {
     if (sum.length > arguments.length) {
       console.log('形参多了')
     } else if (sum.length < arguments.length) {
       console.log('实参多了')
     } else {
       console.log('相等');
     }
   }
   sum(11, 2, 3, 4, 5)  //参数传什么都行 只要是变量都可以传递
1
2
3
4
5
6
7
8
9
10

方法

length为一个方法 表示长度 arguments.length 表示实参列表的长度 也就是11 2 3 4 5

# 函数例子

让随便一组数字相加

function sum() {
     var result = 0;
     for (var i = 0; i < arguments.length; i++) {
       result += arguments[i];
     }
     console.log(result);
   }
      sum(1, 2, 3, 4, 5, 6, 7, 8, 9);
//arguments实参列表 [1,2,3,4,5,6,7,8,9];
1
2
3
4
5
6
7
8
9

不定参数 往里面传数组

# return

作用: 1.终止函数 2.返回参数

function sum(a, b) {
     console.log('a');
     return;
     //在这里终止
     console.log('b');
   }
1
2
3
4
5
6
function sum() {
     return 123;
     //既终止函数又返回123
   }
   var num = sum();         //123
   //把sum函数里面的返回值123传到num变量里
1
2
3
4
5
6

# 例子

function myNumber(target) {
     return + target;   //加法调用隐式类型number
   }
   var num = myNumber('123');
   console.log(typeof (num) + " " + num);
//声明一个变量mynumber 参数为target
//返回参数 target 声明字符串123
//隐式转换为number类型加 空格 加 数字123
1
2
3
4
5
6
7
8

typeof 两种写法

一个加括号typeof(num)

一个加空格typeof num