Contents

  • UNDERSTANDING OBJECT
    • What is Object ?
    • Properties
  • OBJECT CREATION
    • The Factory Pattern
    • The Constructor Pattern
    • The Prototype Pattern
    • Combination Constructor/Prototype Pattern
  • INHERITANCE
    • Prototype Chaining
    • Constructor Stealing
    • Combination Inheritance
    • Prototypal Inheritance

1. What is Object ?

  • ECMA-262

    a standard defines the ECMAScript scripting language

  • ECMAScript

    the scripting language standardized by Ecma International in the ECMA-262

  • What is object ? : "Unordered collection of properties each of which contains a primitive value, object, or function."

  • name-value pairs
  var person = {
    name: "Hans",
    age: 26,
    job: "Super Junior Developer",
    sayName: function(){
        console.log(this.name);
    }
  };
        

1. What is Object ?

  • [[Put]] ( first time )
    • creates a spot in the object to store the property
    • The result of calling [[Put]] is the creation of an own property on the object.

  • [[Set]]
    • This operation replaces the current value of the property with the new one.
  var person = {
    name: "Hans",
    age: 26,
    job: "Super Junior Developer",
    sayName: function(){
        console.log(this.name);
    }
  };
  person.name = "Le Roi";
        
  var object1, object2;
  object1 = {
    a: 3.14159
  }
  object2 = object1;
        


The real picture is :

What is instance and references ? :S


  • A variable will hold a reference to an instance of an object.
  • The actual object is an instance


Properties

Data properties


  • Data properties contain a single location for a data value. Values are read from and written to this location
  • Data properties have four attributes describing their behavior :
    • [[Configurable]]
    • [[Enumerable]]
    • [[Writable]]
    • [[Value]]
  • [[Configurable]], [[Enumerable]], [[Writable]] are true for all properties defined directly on an object, as in the previous example.

  • [[Value]] attribute is set to the assigned value.

  • Object.defineProperty().

ECMAScript 5: Object.defineProperty()


  var person1 = {};

  Object.defineProperty(person1, "name", {
    value: "Neo",
    enumerable: true,
    configurable: true,
    writable: true
  });
        
  • Setting configurable to false means that the property cannot be removed from the object.

  • Once a property has been defined as nonconfigurable, it cannot become configurable again.

ECMAScript 5: Object.defineProperty()


  var person = {};
  Object.defineProperty(person, "name", {
    configurable: false,
    value: "Neo"
  });

  Object.defineProperty(person, "name", {
    configurable: true, // ← error here
    value: "Neo"
  }); // this will throw an error
        
  • Note : Internet Explorer 8 was the first version to implement Object .defineProperty(). Unfortunately, the implementation is extremely limited. This method can be used only on DOM objects and can create only accessor properties.

Properties

Accessor properties


  • Accessor Properties contain a combination of a getter function and a setter function (though both are not necessary).
  • Accessor properties have four attributes:
    • [[Configurable]]
    • [[Enumerable]]
    • [[Get]]
    • [[Set]]
  • [[Configurable]], [[Enumerable]] are true for all properties defined directly on an object, as in the previous example.

  • When an accessor property is read from, the getter function [[Get]] is called

  • when an accessor property is written to, the setter function [[Set]] is called.

  • It is not possible to define an accessor property explicitly so you must use Object.defineProperty().

ECMAScript 5: Object.defineProperty()


  var book = { _year: 2004, edition: 1 };
  Object.defineProperty(book, “year”,
  {
    get: function(){ return this._year; },
    set: function(newValue){
      if (newValue > 2004) {
        this._year = newValue;
        this.edition += newValue - 2004;
      }
    }
  });
  book.year = 2005;
  alert(book.edition); //2
        
  • It’s not necessary to assign both a getter and a setter. Assigning just a getter means that the property cannot be written to and attempts to do so will be ignored.

ECMAScript 5 Object.getOwnPropertyDescriptor()


  var descriptor = Object.getOwnPropertyDescriptor(book, "_year");
  alert(descriptor.value);          //2004
  alert(descriptor.configurable);   //true
  alert(typeof descriptor.get);     //"undefined"
  var descriptor = Object.getOwnPropertyDescriptor(book, "year");
  alert(descriptor.value);          //undefined
  alert(descriptor.enumerable);     //false
  alert(typeof descriptor.get);     //"function"
        
  • It’s not necessary to assign both a getter and a setter. Assigning just a getter means that the property cannot be written to and attempts to do so will be ignored.

  console.log(descriptor);
  Object {get: function, set: function, enumerable: false, configurable: false }
        

OBJECT CREATION

The Factory Pattern


The factory pattern is a well-known design pattern used in software engineering to abstract away the process of creating specific objects


  function Person(name, age, job) {
  return {
      name : name,
      age  : age,
      job  : job,
      run  : function() { alert(name + " is running!"); }
    }
  }
  var person1 = Person("Hans")
  person1.run();
  person1 instanceof Object;   //true
  person1 instanceof Person;   //false
        

Meme Dinosaur



"Oh my god, I’m not a person ... but I can run. What am I really ?"

OBJECT CREATION

The Constructor Pattern


  function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){ alert(this.name); };
  }
  var person1 = new Person(“Le Roi”, 27, “Super Star Singer”);
  var person2 = new Person(“Le Hai Thinh”, 26, “Psychology Doctor”);
  alert(person1.constructor === Person); //true 
  alert(person2.constructor === Person); //true
        
  • There is no object being created explicitly.

  • The properties and method are assigned directly onto the this object.

  • There is no return statement.

OBJECT CREATION

The Constructor Pattern


  alert(person1 instanceof Object); //true
  alert(person1 instanceof Person); //true
  alert(person2 instanceof Object); //true
  alert(person2 instanceof Person); //true
        
  • All custom objects inherit from Object.

  • Constructors are just functions

  • Function that is called with the new operator acts as a constructor

OBJECT CREATION

The Constructor Pattern


  //use as a constructor
  var person = new Person("Le Roi", 26, "Singer");
  person.sayName(); //"Le Roi"

  //call as a function
  Person("Le Tuon Roi", 27, "Graphic Designer of NAU"); //adds to window
  window.sayName(); //"Le Tuon Roi"

  //call in the scope of another object
  var o = new Object();
  Person.call(o, "Kristen", 25, "Nurse");
  o.sayName(); //"Kristen"

  o instanceof Person // "false"
        

OBJECT CREATION

The Constructor Pattern


  function Person(name, age, job){ this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = new Function(“alert(this.name)”);  //logical equivalent
  }

  alert(person1.sayName === person2.sayName); //false
        

OBJECT CREATION

The Constructor Pattern


  function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = sayName;
  }
  function sayName(){
    alert(this.name);
  }
        

OBJECT CREATION

The Prototype Pattern


  • Each function is created with a prototype property, which is an object containing properties and methods that should be available to instances of a particular reference type.

  • This object is a prototype for the object to be created once the constructor is called.

  • All of its properties and methods are shared among object instances.

  • By default, all prototypes automatically get a property called constructor that points back to the function on which it is a property

  • There is no standard way to access [[Prototype]] from script, but Firefox, Safari, and Chrome all support a property on every object called __proto__., in other implementations, this property is completely hidden from script.

OBJECT CREATION

The Prototype Pattern


  function Person(){ }
  Person.prototype.name = "Hans";
  Person.prototype.age = 26;
  Person.prototype.job = "Super Junior";
  Person.prototype.sayName = function(){
    alert(this.name);
  };

  var person1 = new Person();
  person1.sayName(); //"Hans"

  var person2 = new Person();
  person2.sayName(); //"Hans"

  alert(person1.sayName === person2.sayName); //true
        

OBJECT CREATION

The Prototype Pattern


OBJECT CREATION

The Prototype Pattern


  function Person(){ }
  Person.prototype.name = “Thang”;
  Person.prototype.age = 26;
  Person.prototype.job = “Minior”;
  Person.prototype.sayName = function(){
    alert(this.name);
  };
  var person1 = new Person();
  var person2 = new Person();

  person1.name = “Le Roi”;

  alert(person1.name);   //”Le Roi” - from instance
  alert(person2.name);   //” Thang” - from prototype

  delete person1.name;
  alert(person1.name);   //”Thang” - from the prototype

  // Yeah, we should remove Le Roi, I dun like that name.
        

OBJECT CREATION

The Prototype Pattern

hasOwnProperty()


Returns true only if a property of the given name exists on the object instance, as in this example:


  alert(person1.hasOwnProperty(“name”));  //false
  person1.name = “Trang”;
  alert(person1.name); //”Trang” - from instance
  alert(person1.hasOwnProperty(“name”));  //true
        

in operator


Returns true when a property of the given name is accessible by the object, which is to say that the property may exist on the instance or on the prototype.


  alert(person2.hasOwnProperty(“name”)); //false
  alert(“name” in person2); //true
        

OBJECT CREATION

The Prototype Pattern


  function Person(){
  }
  Person.prototype = {
      name : “Nicholas”,
      age : 29,
      job : “Software Engineer”,
      sayName : function () {
          alert(this.name);
      }
  };
        

But


  var friend = new Person();
  alert(friend instanceof Object); //true
  alert(friend instanceof Person); //true
  alert(friend.constructor === Person); //false
  alert(friend.constructor === Object); //true
        

OBJECT CREATION

The Prototype Pattern


  function Person(){ }
  Person.prototype = {
    constructor: Person,
    name : “Nicholas”,
    age : 29,
    job : “Software Engineer”,
    sayName : function () {
      alert(this.name);
    }
  };
        

Using Object.defineProperty()


  //ECMAScript 5 only – restore the constructor
  Object.defineProperty(Person.prototype, “constructor”, {
    enumerable: false,
    value: Person
  });
        

OBJECT CREATION

The Prototype Pattern


  function Person(){
  }
  var friend = new Person();
  Person.prototype = {
    constructor: Person,
    name : “Nicholas”,
    age : 29,
    job : “Software Engineer”,
    sayName : function () {
      alert(this.name);
    }
  };
  friend.sayName(); //error
        

Why it throws error ?

OBJECT CREATION

The Prototype Pattern


OBJECT CREATION

The Prototype Pattern


  function Person(){}
  Person.prototype = {
    constructor: Person,
    name : “Le Roi”,
    age : 29,
    job : “Singer”,
    friends : [“Thuy TOP”, “Phuong Trinh”],
    sayName : function () {
      alert(this.name);
    }
  };
  var person1 = new Person();
  var person2 = new Person();

  person1.friends.push(“Phi Thanh Van”);

  alert(person1.friends);    //   ”Thuy TOP,Phuong Trinh,Phi Thanh Van”
  alert(person2.friends);    //   ”Thuy TOP,Phuong Trinh,Phi Thanh Van”
  alert(person1.friends === person2.friends);  //true
        

OBJECT CREATION

Combination Constructor/Prototype Pattern


  function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.friends = [“Trang”, “Vu”];
  }
  Person.prototype = {
    constructor: Person,
    sayName : function () {
      alert(this.name);
    }
  };
  var person1 = new Person(“Hans”, 26, “Terrorist”);
  var person2 = new Person(“Thang”, 23, “Cop”);

  person1.friends.push(“Thang”);

  alert(person1.friends); //” Trang,Vu,Thang”
  alert(person2.friends); //” Trang,Vu”
  alert(person1.friends === person2.friends); //false
  alert(person1.sayName === person2.sayName); //true
        

INHERITANCE

Prototype Chaining


INHERITANCE

Prototype Chaining


Oppz ! Not that guy, I mean Prototype Chaining, not him...


INHERITANCE

Prototype Chaining


  function SuperType(){
    this.property = true;
  }
  SuperType.prototype.getSuperValue = function(){
     this.property;
  };
  function SubType(){
    this.subproperty = false;
  }

  //inherit from SuperType
  SubType.prototype = new SuperType();
  SubType.prototype.getSubValue = function (){
    return this.subproperty;
  };
  var instance = new SubType();
  alert(instance.getSuperValue()); //true
        

INHERITANCE

Prototype Chaining


INHERITANCE

Prototype Chaining


INHERITANCE

Prototype Chaining

  function SuperType(){
    this.property = true;
  }
  SuperType.prototype.getSuperValue = function(){
    return this.property;
  };
  function SubType(){
    this.subproperty = false;
  }
  //inherit from SuperType
  SubType.prototype = new SuperType();
  //try to add new methods - this nullifies the previous line
  SubType.prototype = {
   getSubValue : function (){
      return this.subproperty;
    },
    someOtherMethod : function (){
      return false;
    }
  };
  var instance = new SubType();
  alert(instance.getSuperValue()); //error!
        

INHERITANCE

Prototype Chaining

  function SubType(){ }
  //inherit from SuperType
  SubType.prototype = new SuperType();

  var instance1 = new SubType();
  instance1.colors.push(“black”);
  alert(instance1.colors); //”red,blue,green,black”

  var instance2 = new SubType();
  alert(instance2.colors); //”red,blue,green,black”
        

INHERITANCE

Constructor Stealing

  function SuperType(){
    this.colors = [“red”, “blue”, “green”];
  }
  function SubType(){
    //inherit from SuperType
    SuperType.call(this);
  }
  var instance1 = new SubType();
  instance1.colors.push(“black”);
  alert(instance1.colors); //”red,blue,green,black”

  var instance2 = new SubType();
  alert(instance2.colors); //”red,blue,green
        

INHERITANCE

Constructor Stealing

  function SuperType(name){
    this.name = name;
  }
  function SubType(){
    //inherit from SuperType passing in an argument
    SuperType.call(this, “Le Roi”);
    //instance property
    this.age = 29;
  }
  var instance = new SubType();
  alert(instance.name); //”Le Roi”;
  alert(instance.age); //29
        

INHERITANCE

Combination Inheritance

  function SuperType(name){
    this.name = name;
    this.colors = [“red”, “blue”, “green”];
  }
  SuperType.prototype.sayName = function(){
    alert(this.name);
  };
  function SubType(name, age){
    //inherit properties
    SuperType.call(this, name);
    this.age = age;
  }
  //inherit methods
  SubType.prototype = new SuperType();
  SubType.prototype.sayAge = function(){
    alert(this.age);
  };
  var instance1 = new SubType(“Le Roi”, 26);
  instance1.colors.push(“black”);
  alert(instance1.colors); //”red,blue,green,black”
  instance1.sayName(); //”Le Roi”;
  instance1.sayAge(); //26
  var instance2 = new SubType(“Le Coc”, 27);
  alert(instance2.colors); //”red,blue,green”
  instance2.sayName(); //”Le Coc”;
  instance2.sayAge(); //27
        

INHERITANCE

Prototypal Inheritance



INHERITANCE

Prototypal Inheritance

  function object(o){
    function F(){}
    F.prototype = o;
    return new F();
  }

  var person = {
    name: “Douglas”,
    friends: [“Quy”, “Thanh”, “Thinh”]
  };

  var anotherPerson = object(person);
  anotherPerson.name = “Le Roi”;
  anotherPerson.friends.push(“Vu”);

  var yetAnotherPerson = object(person);
  yetAnotherPerson.name = “Linda Kieu”;
  yetAnotherPerson.friends.push(“Thang”);

  alert(person.friends); //”Quy,Thanh,Thinh,Vu,Thang”
        

INHERITANCE

Prototypal Inheritance

  var person = {
    name: “Douglas”,
    friends: [“Quy”, “Thanh”, “Thinh”]
  };

  var anotherPerson = Object.create(person);
  anotherPerson.name = “Le Roi”;
  anotherPerson.friends.push(“Vu”);

  var yetAnotherPerson = Object.create(person);
  yetAnotherPerson.name = “Linda Kieu”;
  yetAnotherPerson.friends.push(“Thao”);
  alert(person.friends); //” Quy,Thanh,Thinh,Vu,Thao”
        

<Thank You!>

Important contact information goes here.