POSTS
Read Zackas Object Oriented Programming in JavaScript
BlogI recently read Nicholas Zackas’ The Principles of Object Oriented Javascript and I really recommend it. Many people I know (and students I teach) struggle with mapping the concept of object orientation onto a language that does not follow classical inheritance and lacks many of the visible signals that developers recognize as OO-ish.
The books makes forays into hidden attributes of the language but remains friendly throughout. It offers practical tools and exploratory exercises that will help JavaScript programmers learn more about this interesting and protean language.
I wrote the following precis of my take-aways:
- Introduction: OO Languages
- Encapsulation
- Aggregation (an object can reference another)
- Inheritance
- Polymorphism
- Javascript has these, but they are implemented in a way that appears different
- Primitive and Reference Types
- Primitive types: simple data
- Reference types: stored as objects
- Autoboxing casts primitive types to reference types
- Primitive Types
1. Boolean
1. Number
1. String
1. Null: A primitive type with only one value, null
1. Undefined: Primitive type with only value undefined, value assigned to
uninitialized value
1. A variable holding a primitive holds the value
1. IDENTIFY WITH:
typeof
operator 1. SCREWBALL:typeof null // => "object"
1. Identify nullity by=== null
1. Primitives are autoboxed to provide them an OO interface, but they are not objects - Reference Types
1. Represent objects in JavaScript
1. Reference values are instances of reference types
1. An object is an unordered list of properties consisting of a name and a
value
1. When the value points to a function it is called a method
1. Creating Objects
- Use
new
- Assigned variable holds a pointer
1. Destroying can be accomplished by assigning
null
and letting the GC clean 1. Properties can be added or removed at any time.
- Use
- Instantiating Built-in Types
1. Array, Date, Error, Function, Objecdt, RegExp
1. Many of them have literal forms: Object, Array, RegExp
1. Object literal does not actually fire
new Object()
- Property Acess 1. Dot notation 2. Bracket-and-String
- Identifying Reference Types:
instance instanceof Class
- Identifying Arrays
Array.isArray()
- Primitives are wrapped (autoboxed)
- Functions
- Functions have an internal property named
[[Call]]
- Presence of
[[Call]]
meanstypeof
returns “function” - Declarations v. Expressions
1. Declaration:
function foo()
1. Expression:var x = function()
1. Function declarations are hoisted and thus can be used before definition - Functions can be used as values
- Parameters
1. You can pass any number of parameters
1.
arguments
object exists with all passed parameters 1.arguments
is not an Array 1. Overloading function definitions is not possible 1.this
: The currrent execution context- Can be changed by three means
call
apply
bind
- Understanding Objects
- Defining Properties
1. When a property is put on an object,
[[Put]]
is called- Allocates the memory
- Specified initial value and attributes about the property
- This results in an own property
1. When a property is updated on an object,
[[Set]]
is called - Replaces the current value 1. Detecting Properties
- Use the
in
operator:"name" in person1
- May want to test
person1.hasOwnProperty("name")
to preclude Prototype tampering 1. Removing properties - Don’t merely set to null
- DO use the
delete
operator
- Enumeration
1. By default all properties you add are enumerable
1. Properties have the internal
[[Enumerable]]
attributes set totrue
1. To get a list of propereties, useObject.keys()
: only returns instance properties! 1. Test whether a property is enumerable withpropertyIsEnumerable()
- Types of properties
1. Two types
- Data properties: contain a value
- Accessor properties: don’t contain a value, but a function that
returns a value (i.e. a “getter”)
- Define getter with:
get propertyName(){}
- Define setter with:
set propertyName(){}
- Define getter with:
- Property Attributes
1. Common
[[Enumerable]]
: Can be enumerated?[[Configurable]]
: Can be changed?- By default all properties are both
- Change with
Object.defineProperty(objectOwner, propertyName, propertyDescriptorObject)
1. Data Property Attributes - To additional properties not found in property attributes
[[Value]]
: Holds the actual value[[Writable]]
: Can be overwritten? 1. Data Property Attributes[[Get]]
: Holds the actual value[[Set]]
: Can be overwritten? 1. Multiple properties can be defined withObject.defineProperties()
- Preventing Object Motification
1.
Object.preventExtension()
sets the[[Extensible]]
flag to false 1. Seal the object withObject.seal()
: existing things can be changed 1. Freeze the object withObject.freeze()
: non-ext; unchangeable - Constructors and Prototypes
- Constructor is a function used with
new
to create an Object 1. Ensures siilarity of properties and methods 1.new
will invoke even with out()
1.instanceof
can determine whether an instance is from a constructor 1. When created withnew
aconstructor
property is set on the instance such thatinstance.constructor === ConstructorFunction
istrue
- Prototype
1.
in
operator returnstrue
for both proto properties and own properties 1. An instance keeps track of its prototype with[[Prototype]]
1. Read prototype withObject.getPrototypeOf()
1. Test if something is prototype withobject.isPrototypeOf(instance)
1. Set prototype withObject.setPrototypeOf()
- Inheritance
- Prototypal Inheritance
- Instances inherit their prototype, the proto inherits from its prototype
- See Listing 1
Listing 1
function Animal(species) {
this.species = species
}
Animal.prototype.eat = function() {
console.log("As a: " + this.species + ", I like to eat, yum yum");
}
function Person(name) {
Animal.call(this, "Homo Sapiens");
this.__proto__ = new Animal
this.name = name;
}
var k = new Person("Chris");
k.eat();