Idex N - No Starch Press

3m ago
952.03 KB
5 Pages
Last View : 11d ago
Last Download : n/a
Upload by : Baylee Stein

The Principles of Object-Oriented JavaScript 2014, Nicholas C. ZakasInde xSymbolsB(braces)for function contents, 18and object properties, 9 (double equals operator), 5 (triple equals operator), 5[ ] (square brackets)for array literals, 10for property access, 11–12[[ ]] (double-square-bracketnotation), 17(underscore), in property nameprefix, 38, 80bind() method, 28Boolean object, 3, 15–16Boolean primitive wrapperbraces ({ }){ }Aaccessor properties, 37–38attributes, 41–43creating, 42–43adding properties, 8anonymous functions, 20, 80apply() method, 27, 75arguments, functions as, 20arguments object, 21arity, 21Array built-in type, 8Array.isArray() method, 13–14array literals, 9–10Array.prototype, modifying, 62arraysidentifying, 13–14passing to apply(), 27assignment expressions, 18attributes of properties, 38–44accessor properties, 41–43data properties, 40–41autoboxing, 14type, 14–15for function contents, 18and object properties, 9bracket notation, for propertyaccess, 11–12built-in object prototypes, 62–63built-in types, instantiating, 8–11Ccall() method, 26–27,[[Call]] property, 1775capitalization, of constructor names, 50for strings, 62capitalize() method,charAt() method, 6classes, JavaScript lack of support for, 1closure functions, 81comparison functions, 20comparison without coercion, 5[[Configurable]] attribute, 39, 42for sealed object, 46console.log function, 51constructor property, 50–51changing by object literalnotation, 59constructors, 6–7, 49–53inheritance, 72–75Object.defineProperty() methodinside, 52private members for, 82–84prototype use with, 57–60purpose of, 51scope-safe, 90–92

The Principles of Object-Oriented JavaScript 2014, Nicholas C. Zakasconstructors (continued)stealing, 75–76subtype, 72, 75–76supertype, 72, 75–76create() method, 70creatingaccessor properties, 42–43objects, 6–7properties, on temporary objects, 15Crockford, Douglas, 69Ddatasharing private, 83–84storing on prototype, 57types. See primitive types;reference types; typesdata properties, 37attributes, 40–41from mixins, 88–89Date built-in type, 8Date object, valueOf() method, 67declarations, vs. expressions, 18–19defineProperties() method, 43–44defineProperty() method, 39–41, 52[[Delete]] operation, for objectproperty, 35delete operator, 35, 48dereferencing, objects, 7–8detecting properties, 33–35dot notation, for property access, 11double equals operator ( ), 5double-square-bracketnotation ([[ ]]), 17Eenumerable propertiesadding to Object.prototype, 69copying between supplier andreceiver, 84[[Enumerable]] property attribute, 39, 42enumeration, 36–37equals operators, double ( ) andtriple ( ), 5Error built-in type, 9errorsfrom constructors in strict mode, 53for primitive wrapper objects, 16event support, adding to objects, 85–87expressions, vs. declarations, 18–1994   Indexattribute, 45–47extensions for objects, preventing, 45[[Extensible]]Ffalsy values, 33first-class functions, 2flexibility of JavaScript, 2for-in loops, 36, 69, 84frames of web pages, passing valuesbetween, 13freeze() method, 47, 61freezing objects, 47frozen objects, prototypemodification and, 61Function constructor, 9, 10, 20function keyword, 18function literals, 10–11functions, 2, 17–29declarations vs. expressions, 18–19hoisting, 18–19overloading, 23–24parameters, 21–22as values, 19–21Ggarbage-collection language,JavaScript as, 7[[Get]] attribute, 41getOwnPropertyDescriptor() method, 44getPrototypeOf() method, 55getter functions, 37–38global object, this to represent, 25Hhash maps, JavaScript objects as, 48hasOwnProperty() method, 34–35, 53,66, 69hoisting functions, 18–19Icondition, 33immediately invoked functionexpression (IIFE), 80inheritance, 65–78constructor, 72–75methods from Object.prototype,66–68between objects, 69–72prototype chaining, 65–69pseudoclassical, 76, 87if

The Principles of Object-Oriented JavaScript 2014, Nicholas C. Zakasoperator, 53testing for property instancewith, 33–34instanceof operator, 12–13temporary objects and, 15instances. See also objectschecking type of, 50–51prototype link to constructor, 60of reference types, 6instantiatingbuilt-in types, 8–11objects, 6primitive wrappers, 16internal property, of functions, 17isArray() method, 13–14isExtensible() method, 45, 46isFrozen() method, 47isPrototypeOf() method, 55, 66isSealed() method, 46inKmethod, 36, 89–90key/value pairs, 48keys()Lproperty, of functions, 21–22literals, 3, 9array, 10function, 10–11object, 9–10regular expression, 11lengthMmemory location, pointer to, 7methods, 6, 24–28adding to arrays, 62primitive, 6privileged, 80prototypes for defining, 57–60for supertypes, accessing, 77mixins, 84–90data properties from, 88–89module patterns, 80–82Nnamesfor constructors,capitalization of, 50multiple functions with same, 23for properties, 80operator, 6, 90–92constructors and, 49, 50, 52instantiating reference types with, 9this object created with, 51null value, 3determining if a value is, 5setting object variable to, 7–8setting property to, 35typeof operator and, 5Number primitive wrapper type, 14–15number type, 3newOObject built-in type, 9Object constructor, 32Object.create() method, 70Object.defineProperties() method,Object.defineProperty() method,43–4439–41, 52Object.freeze() method, 47, 61Object.getOwnPropertyDescriptor()method, 44Object.getPrototypeOf() method, 55Object.isExtensible() method, 45, 46Object.isFrozen() method, 47Object.isSealed() method, 46Object.keys() method, 36, 89–90object literals, 9–10object patterns, 79–92private and privilegedmembers, 80–84Object.preventExtensions() method, 45Object.prototype.isPrototypeOf() method,55, 66Object.prototype prototypemethods inherited from, 66–68modifying, 68–69objects, 2, 6, 31–48creating, 6–7dereferencing, 7–8freezing, 47inheritance, 69–72methods, 24–28modification, preventing, 45–47properties, defining, 32–33property inheritance fromprototype, 65–69reference types as, 2sealing, 45–46Object.seal() method, 45–46, 61overloading functions, 23–24Index   95

The Principles of Object-Oriented JavaScript 2014, Nicholas C. Zakasown propertiesdetermining existence of, 66determining whetherenumerable, 66in operator to check for, 34for objects, 32vs. prototype properties, 55–56Pparameters, 21–22person object, module pattern forcreating, 81pointer to memory location, 7preventExtensions() method, 45preventing object modifications, 45–47primitive methods, 6primitive types, 2, 3–6primitive wrapper types, 14–16private data, sharing, 83–84private members, 80–84for constructors, 82–84privileged members, 80–84properties, 6, 11–12, 80adding or removing, 8copying enumerable, betweenreceiver and supplier, 84–86creating on temporary objects, 15defining, 32–33defining multiple, 43–44detecting, 33–35enumerable, adding toObject.prototype, 69enumeration, 36–37identifying on prototype, 54removing, 35string literals for names, 9types, 37–38property attributes, 38–44changing, 39–40retrieving, 44propertyIsEnumerable() method, 37, 39, 66proto property, 55prototype chaining, 65–69, 71, 74object without, 72overwriting, 73prototype propertiesidentifying, 54vs. own properties, 55–56prototype property, of functions, 53, 72[[Prototype]] property, 54–56, 60–6196   Indexprototypes, 53–63built-in object, 62–63changing, 60–62identifying properties, 54overwriting, 59property inheritance from, 65–69use with constructors, 57–60pseudoclassical inheritance, 76, 87pseudoinheritance, mixins for, 84[[Put]] method, 32–33for data properties, 37Rread-only property, 38receiver, copying enumerableproperties betweensupplier and, 84–86Rectangle constructor, 73–75reference types, 2, 6–8identifying, 12–13reference values, storing on prototype,57–58RegExp built-in type, 9RegExp constructor, 11regular expression literals, 11removing properties, 8, 35retrieving property attributes, 44revealing module pattern, 82Sscope-safe constructors, 90–92sealed objects, prototype modificationand, 61sealing objects, 45–46seal() method, 45–46, 61[[Set]] attribute, 32–33, 41setter functions, 37–38sharing private data, 83–84signatures, function with multiple, 23sort() method, 20square brackets ([ ])for array literals, 10for property access, 11–12Square constructor, 73–75stealing constructors, 75–76strict modefor nonextensible objects, 45for sealed objects, 46string literals, as property names, 9String primitive wrapper type, 14–15

The Principles of Object-Oriented JavaScript 2014, Nicholas C. Zakasstringsmethod, 62conversion of values to,for comparison, 21methods, 6string type, 3substring() method, 6subtype constructors, 72, 75–76sum() function, 21supertypeconstructors, 72, 75–76methods, accessing, 77supplier, copying enumerableproperties betweenreceiver and, 84–86capitalize()Uundefined type, 3underscore ( ), in property nameprefix, 38, 80V[[Value]]valueOf()attribute, 40method, 66, 67valuesfunctions as, 19–21passing, between web pageframes, 13variable object, 2variables, for primitive types, 3–4TWtemporary objects, creatingproperties on, 15this object, 25–26changing value of, 26–28to create length and widthproperties, 76creating with new, 51toFixed() method, 6toLowerCase() method, 6toString() method, 6, 35, 66, 67–68triple equals operator ( ), 5truthy values, 33typeof operator, 4–5, 12types, 2. See also primitive types;reference typeschecking for different, 24checking instance for, 50–51instantiating built-in, 8–11web pages, passing values betweenframes, 13wrapper types, primitive, 14–16[[Writable]] attribute, 40write-only properties, 38Index   97

Object built-in type, 9 Object constructor, 32 Object.create() method, 70 Object.defineProperties() method, 43–44 Object.defineProperty() method, 39–41, 52 Object.freeze() method, 47, 61 Object.getOwnPropertyDescriptor() method, 44 Object.getPrototypeOf() method, 55 Object.isExtensible() method, 45, 46 Object.isFrozen() method, 47 Object.isSealed() method, 46