Skip to Content
JavaScript 深入精要
book

JavaScript 深入精要

by Cody Lindley
December 2013
Intermediate to advanced
160 pages
2h 41m
Chinese
GoTop Information, Inc.
Content preview from JavaScript 深入精要
存取屬性的時候,原始/字面值會被轉換成物件
不要對這個事實感到疑惑:你可以將字串、數字與布林字面值視為具有屬性的物件 [例如
true.toString()
]。當你將這些原始值當成物件,並且試著存取它的屬性時,JavaScript
會從原始值的建構式建立一個包裝物件,讓你可以存取這個包裝物件的屬性與方法。在你
完成屬性存取之後,包裝物件就會被捨棄。透過這種轉換方式,在我們編寫的程式中,原
始值就如同物件一般。事實上,當原始值被視為物件時,JavaScript 會將它轉換成物件,
讓你可以存取它的屬性,並且在它回傳值之後將它變回原始值。你需要瞭解的重點在於整
件事的過程,以及 JavaScript 在幕後為你完成了這件事。
字串:
Live Codehttp://jsddle. net/javascript enlightenment/ kpfNk/
<!DOCTYPE html><html lang="en"><body><script>
// 字串物件被視為物件
var stringObject = new String('foo');
console.log(stringObject.length); // log 3
console.log(stringObject['length']); // log 3
// 當字面值/原始字串被視為物件時,它會轉換成物件
var stringLiteral = 'foo';
console.log(stringLiteral.length); // log ...
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 設計模式

Stoyan Stefanov
HTML5 錦囊妙計

HTML5 錦囊妙計

Christopher Schmitt, Kyle Simpson

Publisher Resources

ISBN: 9789863470120