JS-函数深入

function 它是一条数据【工具包】

  • 如果直接执行这个变量名,那么它会返回自己;
  • 每个函数都有一个自己的默认的return值:undefined;
  • 函数的return,只要一看到return后面都不走了;
  • window只是环境,不是函数;
 function a(){
   	return;
   }
   console.log(a);
   console.log(a());

在这里插入图片描述

var allInput=document.getElementsByTagName('input');
    function show(leo){
    	return function(){
    		console.log(leo);
    	}
    }; 
    for (var i = 0; i < allInput.length; i++) {
    	allInput[i].onclick=show(i);//0,1,2
    }

一、函数的声明方式

  • 弱类型的声明方式,可以任意调用
// 弱类型的声明方式,可以任意调用
function a(){  }; 
  • 强类型的声明方式,只能在下面调用
//强类型的声明方式,只能在下面调用
var a=function(){   }; 

二、报错的两种方式

  • 编写型错误 自己的原因写错了 之前都会执行
  • 编译型错误 都不会执行

三、匿名函数

  1. (function( ){ })( );
  2. (function( ){ }( ));
  3. ~function( ){ }( ); =>return -(x)-1

四、Arguments 不定参

  • Arguments对象不是一个Array。它类似于Array,但除了length属性和索引元素之外没有任何Array属性。
  • 任何函数里面都有arguments
	function leo(){
		var oNode=document.createElement(arguments[0]);
		oNode.innerHTML=arguments[1];
		oNode.style.background=arguments[2];
        oNode.style.width=arguments[3];
        oNode.style.height=arguments[4];
        document.body.appendChild(oNode);
	};
	leo('div','我现在用的是arguments','red','500px','300px');
	

二、函数的递归使用

  • 递归是函数一种自己间接调用给自己使用的方式
    var i=0;
    function leo(){
    	i++;
    	console.log(i);
    	//延时异步
    	setTimeout(leo,1000);
    };
    leo();
  function sky(){
    	document.body.style.background='grey';
    	setTimeout(leo,1000);
    };
    function leo(){
    	document.body.style.background='white';
    	setTimeout(sky,500);
    };
    leo();

三、函数的return用法

  • return语句会终止函数的执行并返回函数的值
  • 任何函数都有默认的return值 undefined
  • 函数的return值可以被打印或者alert看到的
alert(alert(10));//10  undefined

四、函数的封装

<head>
	<title></title>
	<style type="text/css">
		div{
			width: 200px;
			height: 200px;
			background: #ccc;
			display: none; 
		}
		div.show{
			display: block;
		}
		.active{
			background:red; 
		}
	</style>
</head>
<body>
   <input type="button" name="" value="aa" class="active">
   <input type="button" name="" value="bb">
   <input type="button" name="" value="cc">
   <div class="show">我是第一个div</div>
   <div class="">我是第二个div</div>
   <div class="">我是第三个div</div>
</body>
<script type="text/javascript">
	var allInput=document.getElementsByTagName('input');
	var allDiv=document.getElementsByTagName('div');
	
	for (var i=0;i<allInput.length;i++){
		allInput[i].setAttribute('index',i);
        allInput[i].onclick=function(){
        	for(var i=0;i<allInput.length;i++){
        		allInput[i].className='';
        		allDiv[i].className='';
        	};
        	this.className='active';
        	allDiv[this.getAttribute('index')].className='show';
        }
	}

</script>
for (var i=0;i<allInput.length;i++){
	    allInput[i].index=i;
        allInput[i].onclick=function(){
        	for(var i=0;i<allInput.length;i++){
        		allInput[i].className='';
        		allDiv[i].className='';
        	};
        	this.className='active';
        	allDiv[this.index].className='show';
        }
	}
<style type="text/css">
		.outNode div{
			width: 200px;
			height: 200px;
			background: #ccc;
			display: none; 
		}
        span,input{
        	cursor: pointer;
        	margin-left:10px; 
        }
        .toActive{
        	background: red;
        }
		.outNode .show{
			display: block;
		}
</style>

<script type="text/javascript">
  //封装一个选项卡
    function CreateTab(obj){	
    	var outNode=document.createElement('div');
    	outNode.className='outNode';
    	document.body.appendChild(outNode);
    	
    	for (var i = 0;i <obj.topValue.length; i++) {
    		var topNode=document.createElement(obj.topNode);
    		if (i==0) {
    			topNode.className=obj.topClass;
    		};
    		if(obj.topNode=='input'){
    			topNode.type='button';
    			topNode.value=obj.topValue[i];
    		}else{
    			topNode.innerHTML=obj.topValue[i];
    		}
    		outNode.appendChild(topNode);
    	}
        
    	for (var i = 0;i <obj.bottomValue.length; i++) {
    		var bottomNode=document.createElement(obj.bottomNode);
    		if (i==0) {
    			bottomNode.className=obj.bottomClass;
    		}
    		bottomNode.innerHTML=obj.bottomValue[i];
    		outNode.appendChild(bottomNode);
    	}

       var allTop=outNode.getElementsByTagName(obj.topNode);
       var allBottom=outNode.getElementsByTagName(obj.bottomNode);
       for (var i = 0;i<allTop.length; i++) {
          allTop[i].index=i;
          allTop[i].onclick=function(){
          	for (var i = 0;i<allTop.length; i++) {
          		allTop[i].className='';
          		allBottom[i].className='';
          	}
            this.className=obj.topClass;
            allBottom[this.index].className=obj.bottomClass;
          }
       }
    };

  //调用
    CreateTab({
        topNode:'span',
		bottomNode:'div',
		topValue:['昨天','今天','明天','后天'],
		bottomValue:['昨天开心','今天很开心','明天超开心','后天特开心'],
		topClass:'toActive', 
		bottomClass:'show'
     })

     CreateTab({
        topNode:'input',
		bottomNode:'div',
		topValue:['ww','ee','ff','tt'],
		bottomValue:['昨天22开心','今天很33开心','明天超55开心','后天特66开心'],
		topClass:'toActive', 
		bottomClass:'show'
     })

</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值