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

What's hot (20)

PPTX
Advanced JavaScript
Nascenia IT
 
PDF
Object Oriented Programming in JavaScript
zand3rs
 
PPT
Advanced JavaScript
Fu Cheng
 
PPTX
AngularJs $provide API internals & circular dependency problem.
Yan Yankowski
 
PPTX
Javascript basics for automation testing
Vikas Thange
 
ODP
Javascript
theacadian
 
PPTX
AngularJS Architecture
Eyal Vardi
 
PPTX
AngularJs
syam kumar kk
 
PPTX
Workshop 1: Good practices in JavaScript
Visual Engineering
 
PDF
GDayX - Advanced Angular.JS
Nicolas Embleton
 
PDF
JavaScript Inheritance
Jussi Pohjolainen
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
PPTX
AngularJS Services
Eyal Vardi
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PPTX
Angular 2 Architecture
Eyal Vardi
 
PPTX
AngularJS Animations
Eyal Vardi
 
PPTX
AngularJS Compile Process
Eyal Vardi
 
PDF
JavaScript Core
François Sarradin
 
PDF
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
PPTX
Template syntax in Angular 2.0
Eyal Vardi
 
Advanced JavaScript
Nascenia IT
 
Object Oriented Programming in JavaScript
zand3rs
 
Advanced JavaScript
Fu Cheng
 
AngularJs $provide API internals & circular dependency problem.
Yan Yankowski
 
Javascript basics for automation testing
Vikas Thange
 
Javascript
theacadian
 
AngularJS Architecture
Eyal Vardi
 
AngularJs
syam kumar kk
 
Workshop 1: Good practices in JavaScript
Visual Engineering
 
GDayX - Advanced Angular.JS
Nicolas Embleton
 
JavaScript Inheritance
Jussi Pohjolainen
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
AngularJS Services
Eyal Vardi
 
Object Oriented Programming In JavaScript
Forziatech
 
Angular 2 Architecture
Eyal Vardi
 
AngularJS Animations
Eyal Vardi
 
AngularJS Compile Process
Eyal Vardi
 
JavaScript Core
François Sarradin
 
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Template syntax in Angular 2.0
Eyal Vardi
 

Viewers also liked (15)

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

Similar to Java script object model (20)

PPT
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
KEY
Javascript tid-bits
David Atchley
 
PPTX
Oojs 1.1
Rodica Dada
 
PPT
Advanced Javascript
relay12
 
PPT
Advanced Javascript
Manikanda kumar
 
PPT
Advanced Javascript
Adieu
 
PPTX
ES6: Features + Rails
Santosh Wadghule
 
PPT
JavaScript - Programming Languages course
yoavrubin
 
PPTX
Getting started with ES6 : Future of javascript
Mohd Saeed
 
PPT
Javascript Object Oriented Programming
Bunlong Van
 
PDF
Prototype
Aditya Gaur
 
PPTX
Ajaxworld
deannalagason
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PDF
ECMAScript 6 new features
GephenSG
 
PPTX
All of Javascript
Togakangaroo
 
PPTX
JavsScript OOP
LearningTech
 
PPTX
All of javascript
Togakangaroo
 
KEY
JavaScript Growing Up
David Padbury
 
PPT
Javascript.ppt
NoralieNicol
 
PPT
JavaScript In Object Oriented Way
Borey Lim
 
Beginning Object-Oriented JavaScript
Stoyan Stefanov
 
Javascript tid-bits
David Atchley
 
Oojs 1.1
Rodica Dada
 
Advanced Javascript
relay12
 
Advanced Javascript
Manikanda kumar
 
Advanced Javascript
Adieu
 
ES6: Features + Rails
Santosh Wadghule
 
JavaScript - Programming Languages course
yoavrubin
 
Getting started with ES6 : Future of javascript
Mohd Saeed
 
Javascript Object Oriented Programming
Bunlong Van
 
Prototype
Aditya Gaur
 
Ajaxworld
deannalagason
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
ECMAScript 6 new features
GephenSG
 
All of Javascript
Togakangaroo
 
JavsScript OOP
LearningTech
 
All of javascript
Togakangaroo
 
JavaScript Growing Up
David Padbury
 
Javascript.ppt
NoralieNicol
 
JavaScript In Object Oriented Way
Borey Lim
 
Ad

Recently uploaded (20)

PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 

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