JavaScript With语句使用示例

作者:拾光时间:2026-10-29 09:51:43浏览:650

本文介绍JavaScript中的With语句用法,通过简化对象属性访问来优化代码。示例展示了如何使用With语句处理Student对象,并弹出姓名和年龄信息。

JavaScript With语句使用示例

本文通过具体实例讲解JavaScript中With语句的使用方法,供读者参考学习。


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>With语句(类似于VB中的)</title>
<script type="text/javascript">
  function Student(name, age) {
    this.name = name;
    this.age = age;
  }
  var stu = new Student('罗纳尔多', 36);
  with (stu) {
    //With语句(类似于VB中的),用于简化代码
    alert("Name:" + name + ",Age:" + age);
  }
</script>
</head>
<body>
</body>
</html>

希望本文提供的关于JavaScript程序设计的说明能对大家有所帮助。

作者最新文章