JavaScript: the good parts #读书笔记

2016-02-17  本文已影响0人  denzel

</code></pre>
typeof以字符串的方式返回object的类型:
<pre><code>
typeof flight.number // 'number' typeof flight.status // 'string' typeof flight.arrival // 'object' typeof flight.manifest // 'undefined'
</code></pre>
hasOwnProperty返回true,如果object有该属性。
<pre><code>
flight.hasOwnProperty('number') // true flight.hasOwnProperty('constructor') // false
</code></pre>
for in可用来遍历object的属性,可以用typeof来过滤函数属性:
<pre><code>
var name; for (name in another_stooge) { if (typeof another_stooge[name] !== 'function') { document.writeln(name + ': ' + another_stooge[name]); } }
</code></pre>
delete可以用来删除object的属性,删除属性并不影响到prototype的属性
<pre><code>
another_stooge.nickname // 'Moe' // Remove nickname from another_stooge, revealing // the nickname of the prototype. delete another_stooge.nickname; another_stooge.nickname // 'Curly'
</code></pre>
可以定义一个全局变量,作为程序的命名空间,以免污染全局命名空间:
<pre><code>
var MYAPP = {}; MYAPP.stooge = { "first-name": "Joe", "last-name": "Howard" }; MYAPP.flight = { airline: "Oceanic", number: 815, departure: { IATA: "SYD", time: "2004-09-22 14:55", city: "Sydney" }, arrival: { IATA: "LAX", time: "2004-09-23 10:42", city: "Los Angeles" } };
</code></pre>

上一篇 下一篇

猜你喜欢

热点阅读