打印

一个javascript例子:

[复制链接]
795|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2021-1-20 14:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
       
<h1>我的第一个 Web 页面</h1>
<p>我的第一个段落。</p>
<script>
function calculate(op,...data){
        if(op === "+"){
                let result = 0;
                for(let i = 0;i < data.length; i ++){
                        result += data[i];
                }
                return result;
        }
        else if(op === "*"){
                let result = 1;
                for(let i = 0;i < data.length;i ++){
                        result *= data[i];
                }
                return result;
        }
}
document.write(Date());
document.write(calculate("+",1,2,3,4,5,6,7));
document.write(calculate("*",1,2,3,4,5,6,7,8,9));
let arr1=[1,2,3,4];
let arr2=[...arr1];
console.log([...arr1,...arr2]);
function sum_arr(...data){
        let result=0;
        for(let i=0;i<data.length;i++){
                result += data[i];
        }
        return result;
}

console.log(sum_arr(...arr1));
let book = {
        title: "杜甫诗集",
        price:98
}
let bookdetail = {...book,desc:"a fine book"}
console.log(bookdetail);
       
let wide=10;
let high=20;
let size={wide,high};
console.log(size);
var car = {
        color:"red",
        dools:4,
        show:function(){
                console.log(this.color);
        },
        dool_num(){
                console.log(this.dools);
        }
}
let n="mini";
car.show();
car.dool_num();
car["wide"]=wide;
car[n+"car"]=high;
console.log(car);
let {color,dools}=car;
console.log(color);
console.log(dools);
car.wide=22;

({wide}=car);
console.log(wide);
let{color:tcolor}=car;
console.log(tcolor);

</script>
       
</body>
</html>

使用特权

评论回复

相关帖子

沙发
keer_zu|  楼主 | 2021-1-20 15:09 | 只看该作者
箭头函数:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
       
<h1>我的第一个 Web 页面</h1>
<p>我的第一个段落。</p>
<script>
let calculate=(op,...data)=>{
        if(op === "+"){
                let result = 0;
                for(let i = 0;i < data.length; i ++){
                        result += data[i];
                }
                return result;
        }
        else if(op === "*"){
                let result = 1;
                for(let i = 0;i < data.length;i ++){
                        result *= data[i];
                }
                return result;
        }
}
document.write(Date());
document.write(calculate("+",1,2,3,4,5,6,7));
document.write(calculate("*",1,2,3,4,5,6,7,8,9));
let arr1=[1,2,3,4,5];
let arr2=[...arr1];
console.log([...arr1,...arr2]);
//function sum_arr(...data){
let sum_arr=(...data)=>{
        let result=0;
        for(let i=0;i<data.length;i++){
                result += data[i];
        }
        return result;
}

console.log(sum_arr(...arr1));
let book = {
        title: "杜甫诗集",
        price:98
}
let bookdetail = {...book,desc:"a fine book"}
console.log(bookdetail);
       
let wide=10;
let high=20;
let size={wide,high};
console.log(size);
var car = {
        color:"red",
        dools:4,
        show:function(){
                console.log(this.color);
        },
        dool_num(){
                console.log(this.dools);
        },
        engine:{
                type:"elect",
                power:20
        }
}
let n="mini";
car.show();
car.dool_num();
car["wide"]=wide;
car[n+"car"]=high;
console.log(car);
let {color,dools}=car;
console.log(color);
console.log(dools);
car.wide=22;

({wide}=car);
console.log(wide);
let{color:tcolor}=car;
console.log(tcolor);
let{engine:{type:etype}}=car;
console.log(etype);
       
let putout=(user,msg)=>`${user}  result:${msg}`;
console.log(putout("zhangsan"," pass"));

let creatcar=(color,doors)=>({color:color,doors:doors});
console.log(creatcar("blue",13));
</script>
       
</body>
</html>

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1352

主题

12436

帖子

53

粉丝