Skip to Content
JavaScript 設計模式
book

JavaScript 設計模式

by Stoyan Stefanov
March 2012
Intermediate to advanced
244 pages
4h 42m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 設計模式
實字與建構式
   
51
雖然這個行為是合理的,因為陣列確實是物件,但卻沒什麼幫助。通常你需要知道一個值
究竟是不是陣列。有時候你可以用程式來檢查
length
屬性是否存在或檢查某些陣列的方
法,例如
slice()
,來決定一個物件的「陣列性」。但這些檢查都不夠健全,因為沒有理
由可以保證一個非陣列的物件不可以有相同名稱的屬性或方法。同樣的,人們有時候會用
instanceof Array
,但這檢查在遇到跨頁框(frames)的時候,在某些 IE 版本會失敗。
ECMAScript 5 定義了一個新的方法
Array.isArray()
,如果傳遞的參數是陣列,它會回傳
true
,例如:
如果你的環境裡不能使用這個新方法,你可以利用呼叫
Object.prototype.toString()
來檢查。當你對一個陣列呼叫
toString
call()
方法,應會 回傳 字串“ [o bj e ct
Array]”。如果對象是一個物件,則應回傳字串“[object Object]”。所以你可以這樣做:
[48]
JSON
現在你已經熟悉了陣列和物件實字,讓我們看看 JSONJavaScript Object Notation),
它是一種資料傳輸格式。它在許多語言都是輕量又方便使用,特別是在 JavaScript
關於 JSON 的一切其實你都已經學會了。它只是陣列實字和物件實字的組合。這是個
JSON 字串的實例:
JSON 和物件實字在語法上唯一的不同是屬性名稱需要用
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript 之美

JavaScript 之美

Anton Kovalyov
重構 JavaScript

重構 JavaScript

Evan Burchard

Publisher Resources

ISBN: 9789862764190