JS-07-JS控制语句与数组

JS-07-JS控制语句与数组

1.JS中有哪些控制语句?

JS中有如下控制语句:

  • 1.if
  • 2.switch
  • 3.for
  • 4.while
  • 5.do…while
  • 6.continue
  • 7.break
  • 8.for…in(了解)
  • 9.with(了解)

用法与java一致。

2.JS数组

2.1JS中数组的创建

JS中数组的元素类型随意,个数随意,用中括号包含元素。

var arr = [1,3.14,"abc",true];

2.2遍历JS数组

用普通for循环遍历JS数组

<script type="text/javascript">
    //数组的创建
    var arr = [1,3.14,"abc",true];
    //用普通for循环遍历数组
    for(var i = 0;i < arr.length;i++) {
        alert(arr[i]);
    }
</script>

3.for…in语句

3.1for…in语句可以遍历JS数组

<script type="text/javascript">
    //数组的创建
    //用for循环遍历
    var arr = [1,3.14,"abc",true];
    //用for...in语句遍历
    for(var i in arr) {
        alert(arr[i]);
    }
</script>

3.2for…in语句还可以获取对象中的属性值

 <script type="text/javascript">
     function User(username,password) {
     	this.username = username;
     	this.password = password;
 	}
	//创建对象
	var user1 = new User("张三",123);
    //获取对象的值:
    alert(user1["username"] + "," + user1["password"]);
    //使用for...in获取对象的值
    for(var i in user1) {
        //alert(i);//属性名username、password
        //alert(typeof i);//数据类型string、string。说明i是字符串"username"、"password"
        alert(user1[i])
    }
</script>

4.with语句

<script type="text/javascript">
    function User(username,password) {
    	this.username = username;
    	this.password = password;
    }
    var user1 = new User("张三",123);
    alert(user1.username + "," + user1.password);
    //with语句
    with(user1) {
        //默认user1.username
        alert(username + "," + password);
    }
</script>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TSCCG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值