arguments,foreach循环,重载,回调函数,对象继承

<html>

<body>

    <p id="p"> this is ppp </p>
    <p id="p2"> this is ppp22 </p>
    <p id="p3"> this is ppp33 </p>
    <script>
    //arguments
    //arguments是每一个函数内部的一个对象
    //可以访问实际传递给函数的参数的信息。
    
    /*
    function add(a,b){
        //console.log(add.length);//形参的个数
        //console.log(arguments.length);//实际传过来的参数
        var total = 0;
        for(var i = 0;i< arguments.length;i++){
        
            total += arguments[i];
        }
        return total;
    }
    
    var result = add(1,2,3,4,5);
    var result2 = add(1,2);
    console.log(result);
    console.log(result2);
    
    
    function setStyle(){
        if(arguments.length < 1
            || arguments.length > 2){
            return ;
        }

        
        var pele = document.getElementById("p");
        if(arguments.length == 1){
            return pele.style[arguments[0]] ;
        }else{
            pele.style[arguments[0]] = arguments[1];
            //pele.style.backgroundColor = "red"
        }
        
        
    }
    
    //setStyle("fontSize","18px");
    
    setStyle("backgroundColor","red");
    var styleValue = setStyle("backgroundColor");
    alert(styleValue);
    
    setStyle("fontSize","18px");
    var styleValue2 = setStyle("fontSize");
    alert(styleValue2);
    
    //声明的时候参数的个数与实际调用时无关
    
    
    //foreach循环
    var arr = [100,300,800];
    for(var data in arr){
        //alert(arr[data]); //循环数组时,data是下标
    }
    
    var o ={name:"cj",age:22};
    for(var p in o){
        //alert(p);
        //alert(o[p]);
    }
    
    
    function myForEach(eles,callback){
        for(var i=0;i< eles.length;i++){
        
            callback(i,eles[i]);
        }
    }
    
    var alls = document.getElementsByTagName("p");
    myForEach(alls,function(index,ele){
        //alert(index);
        //alert(ele);
        ele.style.backgroundColor = "red";
    });*/
    

    
    
    
    
    
    //重载(个数不同,类型不同)
    /*
    function prop(){
    
        var firstP = document.getElementById("p");
        
        if(arguments.length == 1){
            var temp = arguments[0];
            
            for(p in temp) {        
                firstP.style[p] = temp[p];            
            }
            
            
        }else if(arguments.length == 2) {
        
            firstP.style[arguments[0]] = arguments[1];
        
        }
        
    
    }
    
    //prop("backgroundColor","black");
    //prop("fontSize","38px");
    prop({
        backgroundColor:"red",
        fontSize:"38px"
    });
    
    */
    
    /*
    
    
    
    */
    //回调函数
    //js里面,狭隘的理解,参数作为参数,并在函数
    //实现体里面被调用
    
    /*
    function a(){
        b(); //c();d();
    }
    function a(callback){
    
        callback();
        
    }
    function b(){
    
    }
    
    function c(){
    
    }
    a(b);
    a(c);
    a(function(){});
    
    function CjOneDay(maiyan){
        console.log("chouyan");
        
        var yan = maiyan(10);
        console.log(yan);
    }
    
    function banZhangMaiYan(money){
        if(money == 20){
            return "wuyeshen";
        }else{
            return "zhongnanhai";
        }
    }
    CjOneDay(banZhangMaiYan);
    
    
    //
    function myForEachBgColor(eles){
        
        for(var i=0;i< eles.length;i++){
            
            if(i % 2 == 0 ) {
                eles[i].style.backgroundColor = "red";
            }else{
                eles[i].style.backgroundColor = "green";
            }
        }
    }
    
    function myForEachFontSize(eles){
        
        for(var i=0;i< eles.length;i++){
            
            if(i % 2 == 0 ) {
                eles[i].style.fontSize = "30px";
            }else{
                eles[i].style.fontSize = "10px";
            }
        }
    }
    
    function myForEach(eles,callback){
        
        for(var i=0;i< eles.length;i++){
            
            callback(i,eles[i]);
        }
    }
    var allp = document.getElementsByTagName("p");
    //myForEachBgColor(allp);
    //myForEachFontSize(allp);
    
    
    myForEach(allp,function(index,ele){
        if(index % 2 == 0 ) {
                ele.style.fontSize = "30px";
            }else{
                ele.style.fontSize = "10px";
            }
    });
    
    myForEach(allp,function(index,ele){
        if(index % 2 == 0 ) {
                ele.style.backgroundColor = "yellow";
            }else{
                ele.style.backgroundColor = "green";
            }
    });
    
    */
    
    
        //对象继承            
        var child ={name:"banzhang"};
        var parent = {age:18,gender:true}
        
        function myExtend(cObject,pObject) {
        
            for(var p in pObject){
            
                cObject[p] = pObject[p];
            }
            return cObject;
        }
        
        var exChild = myExtend(child,parent);
        //alert(exChild);
        console.log(exChild);
        
        var exChild = superExtend(child,parent,p2,p3,p4);
        
    </script>
</body>
</html>

转载于:https://www.cnblogs.com/luowenjun-kio/p/7899952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值