OOP

JSON

  • JavaScript Object Notation
  • A subset of the JavaScript object literal notation
  • Useful for sending structured data; simpler and faster than using XML
  • Can be used for CSI (client-side includes)
myObj = { name1:value1, name2:value2, ... }
var myObj = {
myInt: 1,
myArray1: ["yellow", "green"],
myArray2: {"lemon":"yellow", "lime":"green"}
}

Object literals

  • JavaScript object literal notation
  • Encapsulated by curly brackets with zero or more property and value pairs, separated by commas
  • Each property and value is separated by a colon
  • Property names can be identifiers, strings, or numbers
  • Values can be strings, numbers, functions, or other objects
var myObj = {
// Properties
myInt: 1,
  myStr: "",
  myBool: false,

  myArray1: [],
myArray2: ["yellow", "green"],
myArray3: {0:"yellow", 1:"green"},
myArray4: {"lemon":"yellow", "lime":"green"},

// Methods
  foo: function() { return this.myInt; },
  bar: function(args) { ... }
}

var x = myObj.foo();
var y = myObj["foo"]();

Object templates

  • aka object constructors, constructor functions
  • Functions can be used as a template for new objects
  • An object template is often referred to as a class
function MyObject(args) {
// Properties
this.myInt = 1;
this.myArray = new Array();

// Methods
this.foo = function() { return this.myInt; }
this.bar = function(args) { ... }
}

# Instantiating the Object
var myObj = new MyObject();
var x = myObj.foo();

Instantiating objects

  • The following are equivalent:
var myObj = new Object();
myObj.num = 30;
myObj.foo = function() {};
// Object template
function MyObject() {
this.num = 30;
this.foo = function() {}
}
var myObj = new MyObject();
// Object literal
var myObj = {
num: 30,
foo: function() {}
}
  • When using the new operator, parentheses are optional when calling an object template with no arguments
function MyObject() {...}

obj1 = new MyObject();
obj2 = new MyObject;

Properties and methods

Public properties

  • Public properties can be accessed within a public or privileged method using "this"
  • Public properties can be accessed externally using the name of the object instance
this.data = 1;

Public methods

  • Added for each instance of the object, even those that have already been created
  • Are not static
  • Public methods can be accessed within a public or privileged method using "this"
  • Public methods can be accessed externally using the name of the object instance
  • Public methods can access public, static, and prototype properties
  • Public methods can access public, privileged, and static methods
  • Public methods are unable to access private properties and methods
MyObject.prototype.foo = function() {...}

Privileged methods

  • Privileged methods can be accessed within public or privileged methods using "this"
  • Privileged methods can be accessed externally using the name of the object instance
  • Privileged methods can access public, private, static, and prototype properties
  • Privileged methods can access public, privileged, private, and static methods
  • Unlike public methods, privileged methods are able to access private properties and methods
this.methodName = function() {...}

Private properties and methods

  • Private properties and methods can be accessed within a privileged or private method using "this"
  • Private methods can access private and static properties and methods
var myVar = 50; 
function foo1() {...}
var foo2 = function() {...}

Static properties and methods

  • Static properties and methods can be accessed within a public, privileged, private, or static method using the name of the object template
  • Static properties and methods can be accessed externally using the name of the object template
  • Static methods can access static properties and methods
MyObject.data = 1;
MyObject.foo = function() {...}

Prototype properties

  • Added for each instance of the object, even those that have already been created
  • Are not static
  • Prototype properties can be accessed within a public or privileged method using "this"
  • Prototype properties can be accessed externally using the name of the object instance
MyObject.prototype.data = 1;

Inheritance

function Parent() {}
function Child() {}

Child.prototype = new Parent(); // Specify that Child inherits from Parent
Child.prototype.constructor = Child; // Reset the constructor property; otherwise, instances
// of Child would have a constructor of Parent.
// Call a parent method from within a child method, using the context of the child object.
// Any parameters for the function go after "this".

Parent.prototype.parentFoo.call(this);

Design patterns

Singleton pattern

var obj = new function() { ... }             // Anonymous object template
var obj = new function MyObject() { ... }    // Named object template (easier to debug)
function MyObject() {                        // Private object constructor

 // If the constructor was not called by the getInstance method
if (MyObject.caller != MyObject.getInstance) {
throw new Error("There is no public constructor for MyObject.");
}
...
}

MyObject.__instance__ = null; // Static property

MyObject.getInstance = function() { // Static method
    if (this.__instance__ == null) {
        this.__instance__ = new MyObject();
    }
    return this.__instance__;
}

var obj = MyObject.getInstance(); // Get the singleton object