一、函数的参数
x,就是函数的参数<br /> 函数的参数是局部变量
<script>function show(x){console.log(x)}console.log(x);show(3)</script>
函数(声明一个函数)
1.使用function关键字去声明
function show(){console.log("hello world")}
2.var
var show = function(){console.log("hello");}
3.return
function go(){return "1"}
8.5 函数的参数
1.函数传不定参2.arguments (函数内部的一个对象,接收传递过来的参数)(arguments是一个类数组的对象)3.重载
function go(a){console.log(arguments[1])console.log(a);}go(10,12);
js中没有重载的概念,因为重复声明,下面的会覆盖上面的声明
function go(a){console.log(a);}function go(a,b){console.log(a+b);}go(10);go(10,20);
8.5.1 使用arguments 模拟重载
function go(){if(arguments.length == 1){console.log(arguments[0])}else if(arguments.length == 2){console.log(arguments[0]+arguments[1])}}go(1)go(10,20)
8.5.2 函数的返回值
// 函数return之后,return后面的语句不再执行
// 作用:将函数内部的值返回到外部
function go(){return "hello world";console.log("good")}console.log(go())
8.6 函数和对象
1.对象的方法 #(在方法中,谁执行方法,this就指向谁)2.indexof # arr.indexof (value) 获取数组的下标3.es6 # 键和值(key:value)相同的时候只用写一个4.解构 # (1.左边读取的字段,右手边必须有 2.读取的是右边的第一个层级)
函数在对象中作为方法的语法(三种)
var obj = {name:"shang",sayName(){console.log(this.name)},sayAge:()=>{console.log(18)},saySkill:function(){console.log("javascript")}}
8.6.5 块级作用域
for(let i=0;i<2;i++){console.log(i)}console.log(i)
二、++,—
++放前面先自增,后运算++放后面先运算,再自增
-- 放前面先自减,后运算-- 放后面先运算,再自减
三、function封装
function封装一段特定功能的代码块
<script>go();function go(){console.log("hello world")}// go();</script>
四、快捷键生成div
div.one*4 { hello $ }
<div class="one">hello 1</div><div class="one">hello 2</div><div class="one">hello 3</div><div class="one">hello 4</div>
