你不知道的JavaScript(十)with关键字
2015-05-25 20:52
204 查看
with关键字在JavaScript中不太常用,用来定义一个和对象相关的作用域,在该作用域中可以访问对象的属性或方法而前面无需加上对象名,以达到简化代码的目的。
在上面的例子中我们定义了一个对象,然后使用with语句创建了一个和person对象相关的作用域,在该作用域中,我们引用对象的属性时无需加上对象名。这里的age=>person.age,address=>person.address。
<script type="text/javascript"> var person = { name:"Jane", age:30, address:"china", phone:"133333" } with (person) { age = 31; address = "america"; } alert(person.age);//31 alert(person.address);//america </script>
在上面的例子中我们定义了一个对象,然后使用with语句创建了一个和person对象相关的作用域,在该作用域中,我们引用对象的属性时无需加上对象名。这里的age=>person.age,address=>person.address。
相关文章推荐
- 你不知道的JavaScript(十)with关键字
- 你不知道的JavaScript(十)with关键字
- Javascript中的with关键字
- JavaScript高级程序设计(四): 关键字With的使用
- JavaScript的strict模式与with关键字介绍
- 理解javascript中的with关键字
- javascript中的with关键字
- Javascript中的this、with关键字
- 理解javascript中的with关键字
- javascript 里面 with 关键字
- 理解javascript中的with关键字
- JavaScript的strict模式与with关键字
- 理解javascript中的with关键字
- javascript中的with关键字
- 你所不知道的JavaScript(一)this 关键字
- Javascript中的with关键字
- JavaScript的strict模式与with关键字介绍
- Trigger a button click with JavaScript on the Enter key in a text box
- javascript查找关键字高亮效果
- Mime type with javascript----text/javascript,application/javascript, and appliation/x-javascript