SlideShare a Scribd company logo
JavaScript Object Model

      James S. Hsieh
JavaScript build-in datatype

Primitive
    Number (float), String, Boolean, undefined, null

And Object
    Array, Function, RegExp, Date .... 
Object 
● JavaScript Object is a hash of key and value.
● How to create a Object? {}
● How to assemble a Object?
  var myObject = {
     "!@. #%": "what is that?",
     value: 1,
     abc: { key: 2 },
     fun: function() {
       return myObject;
     }
  };

  myObject.value;       //   1
  myObject.abc;         //   { key: 2 }
  myObject.!@. #%       //   SyntaxError: Unexpected string
  myObject["!@. #%"];   //   "what is that?"
  myObject.fun;         //   function
  myObject.fun();       //   call function
  myObject.xxx;         //   undefined
Array
● JavaScript Array is a object
● How to create a Array? []
● How to assemble a Array?
 var myArray = [1, "x", function() { return myArray; }];

 myArray[2];       // return element of array
 myArray.length;      // 3
 myArray.abc = "xxx"; // add member to array
 typeof myArray;      // "object"
Function
● JavaScript Function is a object.
● How to create a Function function() {}
● Function object is a invokable.
● All Functions return a value.
● Default is undefined, it can return any object.

● this is execution context.
    ○ If function object is invoked by a.fun();
      execution context of fun is a (this == a)
    ○ If function object is invoked by fun();
      execution context of fun is DOMWindow
    ○ If function object is invoked by new fun();
      .....
Constructor - assemble an object
     ● when invoked with new, functions return an object known
       as this. You can return other object to replace this.
     ● You have a chance of modifying this before it's returned.

        var Engineer = function(name) {
            this.name = name;
            this.favoriteLanguage = "JavaScript";
            this.program = function() {
                return this.createBug();
            };
            this.createBug = function() {
                    /* .... */                           
            };
        };

          var james = new Engineer("James S. Hsieh");        // prototype an object
          james.name;                                                        // "James S. Hsieh"
          james.program();                                                 // call function
          james.constructor == Engineer;                            // true
Constructor - Questions
        var Engineer = function(name) {
            this.name = name;
            this.favoriteLanguage = "JavaScript";
            this.program = function() {
                return this.createBug();
            };
            this.createBug = function() {
                    /* .... */                           
            };
        };

        var james = new Engineer("James");
        var chacha = new Engineer("Chacha");

        chacha.program = // overwrite
            function() { /* new function */ };

        chacha.program();   // what?
        james.program();     // what?
        james.hasOwnProperty("program"); // true
Prototype
A prototype is an early sample or model built to test
a concept or process or to act as a thing to be
replicated or learned from.

   var Engineer = function(name) {
       this.name = name;
       this.favoriteLanguage = "JavaScript";
   };

     Engineer.prototype.program = function() {
         return this.createBug();
     };
     Engineer.prototype.createBug = function() {
         /* .... */                           
     };

   var james = new Engineer("James");
  
    ● All objects of Engineer refer one prototype
    ● object.__proto__ === Engineer.prototype
Prototype - Questions
   var Engineer = function(name) {
       this.name = name;
       this.favoriteLanguage = "JavaScript";
   };

     Engineer.prototype.program = function() {
         return this.createBug();
     };
     Engineer.prototype.createBug = function() {
         /* .... */                           
     };
     
    var james = new Engineer("James");
    var chacha = new Engineer("Chacha");

  Engineer.prototype.program = // overwrite
    function() { /* new function */ }; 

  chacha.program();   // what?
  james.program();     // what?
  james.hasOwnProperty("program"); // false
Prototype chain
Prototype can be a chain, and JavaScript
has a build-in mechanism to resolve
property by this chain. It and C++
inheritance are similar.

Employee <- Engineer

     var james = new Engineer();
     james.A();
     james.B();
     james.C();

   var someOne = new Employee();
   someOne.A();
   someOne.B();
Prototype chain
   var Employee = function() {
        this.aaa = "hellow"; 
   };
   Employee.prototype.A = function() { alert("1"); }; 
   Employee.prototype.B = function() { alert("2"); };  

   var Engineer = function() {         
   };

1. Engineer.prototype = Employee.prototype;
    Engineer.prototype.constructor = Engineer;

2. Engineer.prototype = new Employee();
    Engineer.prototype.constructor = Engineer;

3. var tmp = function() {};
    tmp.prototype = Employee.prototype;
    Engineer.prototype = new tmp();
    Engineer.prototype.constructor = Engineer;

   Engineer.prototype.B = function() { alert("3"); };   
   Engineer.prototype.C = function() { alert("4"); }; 
MooTools framework
MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the
intermediate to advanced JavaScript developer. It allows you to write powerful, flexible,
and cross-browser code with its elegant, well documented, and coherent API.

http://mootools.net/docs/core/Class/Class
http://mootools.net/docs/core/Class/Class.Extras

var Animal = new Class({
  initialize: function(name){ 
    this.name = name;
  } 
}); 

var Cat = new Class({
  Extends: Animal, 
  talk: function(){ 
    return 'Meow!'; 
  } 
});

var james = new Animal('james');
var missy = new Cat('Missy');
Reference

Book: JavaScript Patterns

MooTools: http://mootools.net/

Advanced JavaScript: Closures, Prototypes and Inheritance:
http://www.slideshare.net/Sampetruda/advanced-javascript-
closures-prototypes-and-inheritance?
src=related_normal&rel=1188958

More Related Content

PDF
Advanced javascript
PPT
Advanced JavaScript
PDF
Java Script Best Practices
PPTX
AngularJS with TypeScript and Windows Azure Mobile Services
PDF
Rails is not just Ruby
PDF
Excellent
PPTX
AngularJS $Provide Service
Advanced javascript
Advanced JavaScript
Java Script Best Practices
AngularJS with TypeScript and Windows Azure Mobile Services
Rails is not just Ruby
Excellent
AngularJS $Provide Service

What's hot (20)

PPTX
Advanced JavaScript
PDF
Object Oriented Programming in JavaScript
PPT
Advanced JavaScript
PPTX
AngularJs $provide API internals & circular dependency problem.
PPTX
Javascript basics for automation testing
ODP
Javascript
PPTX
AngularJS Architecture
PPTX
AngularJs
PPTX
Workshop 1: Good practices in JavaScript
PDF
GDayX - Advanced Angular.JS
PDF
JavaScript Inheritance
PDF
JavaScript Basics and Best Practices - CC FE & UX
PPTX
AngularJS Services
PPTX
Object Oriented Programming In JavaScript
PPTX
Angular 2 Architecture
PPTX
AngularJS Animations
PPTX
AngularJS Compile Process
PDF
JavaScript Core
PDF
Anonymous functions in JavaScript
PPTX
Template syntax in Angular 2.0
Advanced JavaScript
Object Oriented Programming in JavaScript
Advanced JavaScript
AngularJs $provide API internals & circular dependency problem.
Javascript basics for automation testing
Javascript
AngularJS Architecture
AngularJs
Workshop 1: Good practices in JavaScript
GDayX - Advanced Angular.JS
JavaScript Inheritance
JavaScript Basics and Best Practices - CC FE & UX
AngularJS Services
Object Oriented Programming In JavaScript
Angular 2 Architecture
AngularJS Animations
AngularJS Compile Process
JavaScript Core
Anonymous functions in JavaScript
Template syntax in Angular 2.0
Ad

Viewers also liked (15)

PPT
2008 07 31 Understanding and Using COM Threading Model - An Inconvenient Trut...
PDF
Crash dump analysis - experience sharing
PPT
Slideshare 基本操作教學
PPTX
100個網站規劃必備的知識 整合:使用者體驗設計分享
PDF
認識用戶體驗設計的價值與招募要點 - Recruit UX Talents For Your Team
PDF
User Interview Techniques
PPTX
從開發人員角度十分鐘理解區塊鏈技術
PPT
Js ppt
PDF
產品設計的0到1,與1到1億
PDF
改變行為的設計:一些理論
PDF
用戶體驗設計,從需求到產品落地
PPTX
阿里巴巴只做沒說的秘密
PDF
Hooked Model
PDF
Visual Design with Data
PDF
3 Things Every Sales Team Needs to Be Thinking About in 2017
2008 07 31 Understanding and Using COM Threading Model - An Inconvenient Trut...
Crash dump analysis - experience sharing
Slideshare 基本操作教學
100個網站規劃必備的知識 整合:使用者體驗設計分享
認識用戶體驗設計的價值與招募要點 - Recruit UX Talents For Your Team
User Interview Techniques
從開發人員角度十分鐘理解區塊鏈技術
Js ppt
產品設計的0到1,與1到1億
改變行為的設計:一些理論
用戶體驗設計,從需求到產品落地
阿里巴巴只做沒說的秘密
Hooked Model
Visual Design with Data
3 Things Every Sales Team Needs to Be Thinking About in 2017
Ad

Similar to Java script object model (20)

PPTX
Ian 20150116 java script oop
PPT
Javascript quiz. Questions to ask when recruiting developers.
PPTX
Functions and Objects in JavaScript
KEY
Javascript tid-bits
PPT
Javascript Design Patterns
PDF
Let's JavaScript
PDF
JavaScript and UI Architecture Best Practices
PDF
The Beauty Of Java Script V5a
PDF
Javascript Design Patterns
PPTX
Java script for web developer
PPTX
Intro to Javascript
PPTX
Object Oriented JavaScript
PDF
The Beauty of Java Script
PDF
Secrets of JavaScript Libraries
PDF
Functional Javascript
KEY
Engineering JavaScript
PDF
JavaScript For CSharp Developer
PPTX
Journey of a C# developer into Javascript
PPT
A Deeper look into Javascript Basics
Ian 20150116 java script oop
Javascript quiz. Questions to ask when recruiting developers.
Functions and Objects in JavaScript
Javascript tid-bits
Javascript Design Patterns
Let's JavaScript
JavaScript and UI Architecture Best Practices
The Beauty Of Java Script V5a
Javascript Design Patterns
Java script for web developer
Intro to Javascript
Object Oriented JavaScript
The Beauty of Java Script
Secrets of JavaScript Libraries
Functional Javascript
Engineering JavaScript
JavaScript For CSharp Developer
Journey of a C# developer into Javascript
A Deeper look into Javascript Basics

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
Understanding_Digital_Forensics_Presentation.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation

Java script object model

  • 1. JavaScript Object Model James S. Hsieh
  • 2. JavaScript build-in datatype Primitive     Number (float), String, Boolean, undefined, null And Object     Array, Function, RegExp, Date .... 
  • 3. Object  ● JavaScript Object is a hash of key and value. ● How to create a Object? {} ● How to assemble a Object? var myObject = { "!@. #%": "what is that?", value: 1, abc: { key: 2 }, fun: function() { return myObject; } }; myObject.value; // 1 myObject.abc; // { key: 2 } myObject.!@. #% // SyntaxError: Unexpected string myObject["!@. #%"]; // "what is that?" myObject.fun; // function myObject.fun(); // call function myObject.xxx; // undefined
  • 4. Array ● JavaScript Array is a object ● How to create a Array? [] ● How to assemble a Array? var myArray = [1, "x", function() { return myArray; }]; myArray[2]; // return element of array myArray.length; // 3 myArray.abc = "xxx"; // add member to array typeof myArray; // "object"
  • 5. Function ● JavaScript Function is a object. ● How to create a Function function() {} ● Function object is a invokable. ● All Functions return a value. ● Default is undefined, it can return any object. ● this is execution context. ○ If function object is invoked by a.fun(); execution context of fun is a (this == a) ○ If function object is invoked by fun(); execution context of fun is DOMWindow ○ If function object is invoked by new fun(); .....
  • 6. Constructor - assemble an object ● when invoked with new, functions return an object known as this. You can return other object to replace this. ● You have a chance of modifying this before it's returned.         var Engineer = function(name) {             this.name = name;             this.favoriteLanguage = "JavaScript";             this.program = function() {                 return this.createBug();             };             this.createBug = function() {                     /* .... */                                        };         };         var james = new Engineer("James S. Hsieh");        // prototype an object         james.name;                                                        // "James S. Hsieh"         james.program();                                                 // call function         james.constructor == Engineer;                            // true
  • 7. Constructor - Questions         var Engineer = function(name) {             this.name = name;             this.favoriteLanguage = "JavaScript";             this.program = function() {                 return this.createBug();             };             this.createBug = function() {                     /* .... */                                        };         };         var james = new Engineer("James");         var chacha = new Engineer("Chacha");         chacha.program = // overwrite             function() { /* new function */ };         chacha.program();   // what?         james.program();     // what?         james.hasOwnProperty("program"); // true
  • 8. Prototype A prototype is an early sample or model built to test a concept or process or to act as a thing to be replicated or learned from.    var Engineer = function(name) {        this.name = name;        this.favoriteLanguage = "JavaScript";    };    Engineer.prototype.program = function() {        return this.createBug();    };    Engineer.prototype.createBug = function() {        /* .... */                               };    var james = new Engineer("James");    ● All objects of Engineer refer one prototype ● object.__proto__ === Engineer.prototype
  • 9. Prototype - Questions    var Engineer = function(name) {        this.name = name;        this.favoriteLanguage = "JavaScript";    };    Engineer.prototype.program = function() {        return this.createBug();    };    Engineer.prototype.createBug = function() {        /* .... */                               };       var james = new Engineer("James");   var chacha = new Engineer("Chacha");   Engineer.prototype.program = // overwrite     function() { /* new function */ };    chacha.program();   // what?   james.program();     // what?   james.hasOwnProperty("program"); // false
  • 10. Prototype chain Prototype can be a chain, and JavaScript has a build-in mechanism to resolve property by this chain. It and C++ inheritance are similar. Employee <- Engineer    var james = new Engineer();    james.A();    james.B();    james.C();    var someOne = new Employee();    someOne.A();    someOne.B();
  • 11. Prototype chain    var Employee = function() {         this.aaa = "hellow";     };    Employee.prototype.A = function() { alert("1"); };     Employee.prototype.B = function() { alert("2"); };      var Engineer = function() {             }; 1. Engineer.prototype = Employee.prototype;     Engineer.prototype.constructor = Engineer; 2. Engineer.prototype = new Employee();     Engineer.prototype.constructor = Engineer; 3. var tmp = function() {};     tmp.prototype = Employee.prototype;     Engineer.prototype = new tmp();     Engineer.prototype.constructor = Engineer;    Engineer.prototype.B = function() { alert("3"); };       Engineer.prototype.C = function() { alert("4"); }; 
  • 12. MooTools framework MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. It allows you to write powerful, flexible, and cross-browser code with its elegant, well documented, and coherent API. http://mootools.net/docs/core/Class/Class http://mootools.net/docs/core/Class/Class.Extras var Animal = new Class({   initialize: function(name){      this.name = name;   }  });  var Cat = new Class({   Extends: Animal,    talk: function(){      return 'Meow!';    }  }); var james = new Animal('james'); var missy = new Cat('Missy');
  • 13. Reference Book: JavaScript Patterns MooTools: http://mootools.net/ Advanced JavaScript: Closures, Prototypes and Inheritance: http://www.slideshare.net/Sampetruda/advanced-javascript- closures-prototypes-and-inheritance? src=related_normal&rel=1188958