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
Ad

Recommended

Advanced javascript
Advanced javascript
Doeun KOCH
 
Advanced JavaScript
Advanced JavaScript
Stoyan Stefanov
 
Java Script Best Practices
Java Script Best Practices
Enrique Juan de Dios
 
AngularJS with TypeScript and Windows Azure Mobile Services
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
Rails is not just Ruby
Rails is not just Ruby
Marco Otte-Witte
 
RSpec
RSpec
Marco Otte-Witte
 
Excellent
Excellent
Marco Otte-Witte
 
AngularJS $Provide Service
AngularJS $Provide Service
Eyal Vardi
 
Advanced JavaScript
Advanced JavaScript
Nascenia IT
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
zand3rs
 
Advanced JavaScript
Advanced JavaScript
Fu Cheng
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.
Yan Yankowski
 
Javascript basics for automation testing
Javascript basics for automation testing
Vikas Thange
 
Javascript
Javascript
theacadian
 
AngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
AngularJs
AngularJs
syam kumar kk
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
Visual Engineering
 
GDayX - Advanced Angular.JS
GDayX - Advanced Angular.JS
Nicolas Embleton
 
JavaScript Inheritance
JavaScript Inheritance
Jussi Pohjolainen
 
JavaScript Basics and Best Practices - CC FE & UX
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
AngularJS Services
AngularJS Services
Eyal Vardi
 
Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
Forziatech
 
Angular 2 Architecture
Angular 2 Architecture
Eyal Vardi
 
AngularJS Animations
AngularJS Animations
Eyal Vardi
 
AngularJS Compile Process
AngularJS Compile Process
Eyal Vardi
 
JavaScript Core
JavaScript Core
François Sarradin
 
Anonymous functions in JavaScript
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0
Eyal Vardi
 
2008 07 31 Understanding and Using COM Threading Model - An Inconvenient Trut...
2008 07 31 Understanding and Using COM Threading Model - An Inconvenient Trut...
James Hsieh
 
Crash dump analysis - experience sharing
Crash dump analysis - experience sharing
James Hsieh
 

More Related Content

What's hot (20)

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

Viewers also liked (15)

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

Similar to Java script object model (20)

Ian 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
Alberto Naranjo
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript
Dhananjay Kumar
 
Javascript tid-bits
Javascript tid-bits
David Atchley
 
Javascript Design Patterns
Javascript Design Patterns
Zohar Arad
 
Let's JavaScript
Let's JavaScript
Paweł Dorofiejczyk
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
Siarhei Barysiuk
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
Javascript Design Patterns
Javascript Design Patterns
Subramanyan Murali
 
Java script for web developer
Java script for web developer
Chalermpon Areepong
 
Intro to Javascript
Intro to Javascript
Anjan Banda
 
Object Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
The Beauty of Java Script
The Beauty of Java Script
Michael Girouard
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
Functional Javascript
Functional Javascript
guest4d57e6
 
Engineering JavaScript
Engineering JavaScript
Jim Purbrick
 
JavaScript For CSharp Developer
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
Journey of a C# developer into Javascript
Journey of a C# developer into Javascript
Massimo Franciosa
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
Núcleo de Electrónica e Informática da Universidade do Algarve
 
A Deeper look into Javascript Basics
A Deeper look into Javascript Basics
Mindfire Solutions
 
Ian 20150116 java script oop
Ian 20150116 java script oop
LearningTech
 
Javascript quiz. Questions to ask when recruiting developers.
Javascript quiz. Questions to ask when recruiting developers.
Alberto Naranjo
 
Functions and Objects in JavaScript
Functions and Objects in JavaScript
Dhananjay Kumar
 
Javascript Design Patterns
Javascript Design Patterns
Zohar Arad
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
Siarhei Barysiuk
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
Intro to Javascript
Intro to Javascript
Anjan Banda
 
Object Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
Secrets of JavaScript Libraries
Secrets of JavaScript Libraries
jeresig
 
Functional Javascript
Functional Javascript
guest4d57e6
 
Engineering JavaScript
Engineering JavaScript
Jim Purbrick
 
JavaScript For CSharp Developer
JavaScript For CSharp Developer
Sarvesh Kushwaha
 
Journey of a C# developer into Javascript
Journey of a C# developer into Javascript
Massimo Franciosa
 
A Deeper look into Javascript Basics
A Deeper look into Javascript Basics
Mindfire Solutions
 
Ad

Recently uploaded (20)

Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 

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