与Java中的传参几乎完全一致
~~~
<script type="text/javascript">
fn1(100);
function fn1(a){alert(a);}//100
</script>
~~~
~~~
<script type="text/javascript">
function fn(w,d){
w.onload = function(){
d.body.innerHTML = 123;
}
}
fn(window,document);
</script>
~~~
~~~
<script type="text/javascript">
//函数也能作为参数
fn(function() {
alert(1)
});
function fn(a) {
if(typeof a === 'number' && a === a) { //NaN也是number,a===a就是排除NaN
alert(a + 20);
} else if(typeof a === 'String') {
alert(a.charat(2));
} else
if(typeof a === 'function') {
a();
}
}
</script>
~~~
课堂练习-1:图片切换,那之前做的练习,将其变成2份,利用传参完成
课堂练习-2:模拟购物车点击增加数量
***
课后作业-1:购物车拓展
![](https://box.kancloud.cn/4fdf28136eceab9a21266d7a2ce602cc_586x273.png)
拓展练习:
![](https://box.kancloud.cn/125031149b26f1f55c98879b7a8cb8b5_972x345.gif)