The Principles of Object-Oriented Javascript Zakas
Total Page:16
File Type:pdf, Size:1020Kb
TAKETAKE CONTROLCONTROL OFOF Foreword by Cody Lindley, Best-selling Author and Principal Frontend Architect JAVASCRIPT THE PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT JAVASCRIPT THE PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT THETHE PRINCIPLESPRINCIPLES OFOF OBJECTSOBJECTS at TandemSeven OBJECT-ORIENTEDOBJECT-ORIENTED If you’ve used a more traditional object-oriented • How to define your own constructors JAVASCRIPTJAVASCRIPT language, such as C++ or Java, JavaScript probably • How to work with and understand prototypes doesn’t seem object-oriented at all. It has no concept of classes, and you don’t even need to define any • Inheritance patterns for types and objects objects in order to write code. But don’t be fooled — The Principles of Object-Oriented JavaScript will leave NICHOLAS C. ZAKAS JavaScript is an incredibly powerful and expressive even experienced developers with a deeper understand- object-oriented language that puts many design ing of JavaScript. Unlock the secrets behind how objects decisions right into your hands. work in JavaScript so you can write clearer, more In The Principles of Object-Oriented JavaScript, flexible, and more efficient code. Nicholas C. Zakas thoroughly explores JavaScript’s object-oriented nature, revealing the language’s A B O U T T H E A U T H O R unique implementation of inheritance and other key characteristics. You’ll learn: Nicholas C. Zakas is a software engineer at Box and is known for writing on and speaking about the latest • The difference between primitive and reference in JavaScript best practices. He honed his experience values during his five years at Yahoo!, where he was principal • What makes JavaScript functions so unique frontend engineer for the Yahoo! home page. He is the author of several books, including Maintainable JavaScript • The various ways to create objects and Professional JavaScript for Web Developers. TH E FINEST I N G EEK ENTERTAI N M ENT ™ ZAKAS www.nostarch.com $24.95 ($25.95 CDN) PROGRAMMING/JAVASCRIPT SHELVE IN: SFI-00 THE PRINCIpLES OF OBJECt-ORIENtED JAVASCRIpt THE PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT by Nicholas C. Zakas San Francisco TE H PRINCIPLES OF OBJECT-ORIENTED JAVASCRIPT. Copyright © 2014 by Nicholas C. Zakas. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA First printing 18 17 16 15 14 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-540-4 ISBN-13: 978-1-59327-540-2 Publisher: William Pollock Production Editor: Serena Yang Cover Illustration: Charlie Wylie Interior Design: Octopod Studios Developmental Editor: Jennifer Griffith-Delgado Technical Reviewer: Angus Croll Copyeditor: Rachel Monaghan Compositor: Serena Yang Proofreader: Elaine Merrill Indexer: Nancy Guenther For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; [email protected]; www.nostarch.com Library of Congress Cataloging-in-Publication Data Zakas, Nicholas C. The principles of object-oriented JavaScript / by Nicholas C. Zakas. pages cm Includes index. ISBN-13: 978-1-59327-540-2 (paperback) ISBN-10: 1-59327-540-4 (paperback) 1. JavaScript (Computer program language) 2. Object-oriented programming languages. I. Title. QA76.73.J39Z357 2014 005.1'17--dc23 2013048973 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the infor- mation contained in it. About the Author Nicholas C. Zakas is a software engineer at Box and is known for writing on and speaking about the latest in JavaScript best practices. He honed his experience during his five years at Yahoo!, where he was principal front end engineer for the Yahoo! home page. He is the author of sev- eral books, including Maintainable JavaScript (O’Reilly Media, 2012) and Professional JavaScript for Web Developers (Wrox, 2012). About the Technical Reviewer Originally from the UK, Angus Croll is now part of Twitter’s web frame- work team in San Francisco and is the co-author and principal main- tainer of Twitter’s open source Flight framework. He’s obsessed with JavaScript and literature in equal measure and is a passionate advocate for the greater involvement of artists and creative thinkers in software development. Angus is a frequent speaker at conferences worldwide and is currently working on two books for No Starch Press. He can be reached on Twitter at @angustweets. B RIEF CONtENtS Foreword by Cody Lindley ............................................. xiii Acknowledgments .................................................... xv Introduction ........................................................ xvii Chapter 1: Primitive and Reference Types ..................................... 1 Chapter 2: Functions .................................................. 17 Chapter 3: Understanding Objects......................................... 31 Chapter 4: Constructors and Prototypes ..................................... 49 Chapter 5: Inheritance ................................................. 65 Chapter 6: Object Patterns .............................................. 79 Index ............................................................. 93 CONTENTS IN DETAIL FOREWORD by Cody Lindley xiii ACKNOWLEDGMENTS xv INTRODUCTION xvii Who This Book Is For . .xviii Overview ....................................................... xix Help and Support.................................................. xix 1 PRIMITIVE AND REFERENCE TYPES 1 What Are Types? ................................................... 2 Primitive Types..................................................... 3 Identifying Primitive Types ...................................... 4 Primitive Methods............................................ 6 Reference Types.................................................... 6 Creating Objects ............................................ 6 Dereferencing Objects ........................................ 7 Adding or Removing Properties .................................. 8 Instantiating Built-in Types ............................................. 8 Literal Forms ............................................... 9 Object and Array Literals ...................................... 9 Function Literals ............................................ 10 Regular Expression Literals .................................... 11 Property Access ................................................... 11 Identifying Reference Types ........................................... 12 Identifying Arrays.................................................. 13 Primitive Wrapper Types............................................. 14 Summary ....................................................... 16 2 FUNCTIONS 17 Declarations vs. Expressions .......................................... 18 Functions as Values ................................................ 19 Parameters ...................................................... 21 Overloading ..................................................... 23 Object Methods................................................... 24 The this Object ............................................ 25 Changing this ............................................. 26 Summary ....................................................... 29 3 UNDERSTANDING OBJECTS 31 Defining Properties................................................. 32 Detecting Properties ................................................ 33 Removing Properties ................................................ 35 Enumeration ..................................................... 36 Types of Properties ................................................. 37 Property Attributes ................................................. 38 Common Attributes.......................................... 39 Data Property Attributes ...................................... 40 Accessor Property Attributes ................................... 41 Defining Multiple Properties.................................... 43 Retrieving Property Attributes ................................... 44 Preventing Object Modification ........................................ 45 Preventing Extensions ........................................ 45 Sealing Objects ............................................ 45 Freezing Objects ........................................... 47 Summary ....................................................... 48 4 CONSTRUCTORS AND PROTOTYPES 49 Constructors ..................................................... 49 Prototypes ....................................................... 53 The [[Prototype]] Property ..................................... 54