Mastering JavaScript Object-Oriented Programming - Programmer Books

[Pages:287]

Mastering JavaScript ObjectOriented Programming

UnleashthetruepowerofJavaScriptby masteringObjectOriented programming principles and patterns

Andrea Chiarelli

BIRMINGHAM- MUMBAI



Mastering JavaScript Object-Oriented Programming

Copyright ? 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2016 Production reference: 1220616

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78588-910-3



Credits

Author Andrea Chiarelli

Copy Editor Charlotte Carneiro

Reviewer Lyubomyr Rudko

Project Coordinator Sanchita Mandal

Commissioning Editor Wilson D'souza

Proofreader Safis Editing

Acquisition Editor Larissa Pinto

Indexer Monica Ajmera Mehta

Content Development Editor Shali Deeraj

Technical Editors Gebin George

Production Coordinator Nilesh Mohite



About the Author

Andrea Chiarelli has over 20 years of experience as software engineer and technical writer. Throughout his career, he has used various technologies for the projects he was involved in, ranging from C# to JavaScript, to AngularJS, and REST to PhoneGap/Cordova. He has contributed to many online and offline magazines, such as Computer Programming and ASP Today and has coauthored a few books published by Wrox Press. Currently, he is a senior software engineer at the Italian office of Apparound Inc., a mobile software company founded in the heart of Silicon Valley, and he is a regular contributor to HTML.it, an Italian online magazine focused on web technologies. You can contact him at h t t p s : / / w w w . l i n k e d i n . c o m / i n / a n d r e a c h i a r e l l i

I wish to thank my family for their support, patience and love.



About the Reviewer

Lyubomyr Rudko is Senior Software Engineer, and has been developing large web application with JavaScript for the last 6 years. He has great experience in AngularJS, BackboneJS, React.js and Sencha ExtJS. Lyubomyr is a big fan of JavaScript language and truly enjoys sharing his skills and experience with others. Currently, he is working as a Team Technical Lead for Lohika (). You can contact him at .





eBooks,discountoffers,and more

DidyouknowthatPacktofferseBookversionsofeverybookpublished, withPDFand ePubfilesavailable?YoucanupgradetotheeBookversionat andasa printbookcustomer,youareentitledtoadiscountontheeBookcopy.Getintouch withus atcustomercare@ for moredetails. At,youcanalsoreadacollectionoffreetechnicalarticles,signupfora rangeoffreenewslettersandreceiveexclusivediscountsandoffersonPacktbooksand eBooks.

h t t p s : / / w w w 2 . p a c k t p u b . c o m / b o o k s / s u b s c r i p t i o n / p a c k t l i b DoyouneedinstantsolutionstoyourITquestions?PacktLibisPackt'sonlinedigitalbook library. Here,youcansearch,access,andreadPackt'sentirelibraryofbooks.

Why subscribe?

Fully searchable across every book published by Packt Copyandpaste,print,andbookmarkcontent Ondemandandaccessibleviaa webbrowser



Table of Contents

Preface

1

Chapter 1: A Refresher of Objects

6

Object literals

6

Properties

7

Methods

9

Object constructors

11

The Object() constructor

13

Object prototypes

15

Using classes

16

Summary

19

Chapter 2: Diving into OOP Principles

20

OOP principles

21

Is JavaScript Object Oriented?

22

Abstraction and modeling support

22

Association

23

Aggregation

24

Composition

25

OOP principles support

25

Encapsulation

25

Inheritance

28

Polymorphism

30

JavaScript OOP versus classical OOP

34

Summary

35

Chapter 3: Working with Encapsulation and Information Hiding

36

Encapsulation and information hiding

36

Convention-based approach

37

Privacy levels using closure

38

Scope and closure

39

Privacy levels

42

Benefits and drawbacks

43

A meta-closure approach

44

Immediately invoked function expressions

44

Creating a meta-closure with an IIFE

45



................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download