<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script> /* * for 数组 * for-in 对象 * 避免使用continue,使用条件语句替代 */ switch ('1') { case '1': case '2': console.log('ca'); break; default: console.log('caca'); } /* * 通过with可以用局部变量和函数的形式来访问特定对象的属性和方法, * 这样就可以将对象前缀统统省略掉 * 不推荐使用 */ var book = { title: 'Maintainable JavaScript', author: 'Nicholas C. Zakes' } var message = 'The book is ' with (book) { message += title message += ' by ' + author } console.log(message) /* * for-in不仅遍历对象的实例属性,同样还遍历从原型继承而来的属性 * Crockford的编程规范要求所有的for-in循环都必须使用hasOwnProperty() */ var prop for (prop in book) { if (book.hasOwnProperty(prop)) { console.log('Property name is ' + prop) console.log('Property value is ' + book[prop]) } } </script> </body> </html>