SlideShare uma empresa Scribd logo
1 de 20
Alipay Inc


     Understand Prototype



                      颂赞
             (http://qiqicartoon.com)
Alipay Inc


• What is prototype?
• How to inherit?
• Simple Object
• Defining a Class
• Defining and Calling method in Class
• Calling the SuperClass from SubClass
• Overriding Method of SuperClass in SubClass
• Calling Method of SuperClass from SubClass
Alipay Inc


       What is prototype?
     By ECMASCRIPT: Each constructor is a function
     that has a property named ―prototype that is
     used to implement prototype-based inheritance
     and shared properties.
Alipay Inc


    Prototype is prototype?

     Prototype contain:
     • Prototype property
     • Prototype object
Alipay Inc


       What is prototype?
     function User(){
       //…
     }                    Prototype property
     User.prototype = {
       //methods…
     };
                          Prototype object
Alipay Inc


       What is prototype?
     Prototype object也是类成员的集合
Alipay Inc

       What is prototype?
      Array, Object, Number, Function, Null, Undefind
              function _arrayObj(){}

              Array.prototype = new _arrayObj();
              console.log(Array.prototype.constructor) ;//Array,not Function

              var userName = ‘zhuiq’,
                  companies = [‘alipay’,’alisoft’],
                  friends = {
                      ‘a’:{},’b’:{}
                  };

              userName.prototype = {};//?
              companies.prototype = {};//?
              friends.prototype = {};//?
              null.prototype //?
              undefined.prototype //?
Alipay Inc

       What is prototype?
Javascript原始对象的prototype是只读的!
只有函数对象才有prototype属性!
Alipay Inc

         What is prototype?
                                methods
                                • __defineGetter__
                                • __defineSetter__
                                • eval
                                • hasOwnProperty
                                • isPrototypeOf
                                • valueOf
   Prototype                    • watch
    Object                      • unwatch
                                • toString
                properties      • toSource
                                • propertyIsEnumerable
                • constructor   • __noSuchMethod__
                • __parent__    • __lookupGetter__
                • __proto__     • __lookupSetter__
                • __count__
Alipay Inc


             How to inherit?
PHP: extends
JAVA: extends
Ruby: <
ASP: :
Python: class subClassName(SuperClassName)
C++: :
Javascript: prototype
Alipay Inc


             How to inherit?
Alipay Inc


               How to inherit?
   var CF= function (){},
       CFp = {CFP1:1,CFP2:2};

   CF.prototype = CFp;
   CF.prototype.P1 = ‘P1’;
   CF.prototype.P2 = ‘P2’;

   var cf1 = new CF,
       cf2 = new CF,
       cf3 = new CF,
       cf4 = new CF,
       cf5 = new CF;
Alipay Inc


                   Simple Object
    function User(){
      this.firstName = ‘颂’;
      this.lastName = ‘赞’;
    }
    var user = {firstName:’zhu’,lastName:’qi’};
    User.prototype = user;
    var userByConstructor = new User();

  user                                       userByConstructor
  firstName zhu                              firstName   颂
  lastName qi                                lastName    赞
              prototypeObject? [Object]                  prototypeObject:user
  prototype                                  prototype
              constructor:Object                         constructor:Function
Alipay Inc

   function User(){
     this.sex= ‘male’;
   }
   User.prototype = {
     getSex : function (){return this.sex;}
   };
   function AlipayUser(){
     this.sex = ‘female’;
   }
   function TaobaoUser(){}

   AlipayUser.prototype = new User;
   TaobaoUser.prototype = new User;

   AlipayUser.prototype.constructor = AlipayUser;
   TaobaoUser.prototype.constructor = TaobaoUser;

   var alipay_user = new AlipayUser ();
   var taobao_user = new TaobaoUser;
Alipay Inc


 User
 sex          male

              prototypeObject: Object
 prototype
              constructor:Function      TaobaoUser
                                        sex         male

                                                    prototypeObject: Object
 AlipayUser
                                        prototype
 sex          female                                constructor:TaobaoUser
              prototypeObject: Object
 prototype
              constructor:AlipayUser
Alipay Inc



    console.log(alipay_user .constructor);//AlipayUser

    console.log(taobao_user .constructor);//TaobaoUser

    console.log(alipay_user instanceof AlipayUser );//true;

    console.log(taobao_user instanceof TaobaoUser);//true;

    console.log(
      taobao_user instanceof User
      &&
      alipay_user instanceof User
    );//true
Alipay Inc

  Defining and Calling method in Class
    function TaobaoUser(cfg){
      User.call(this,cfg);
      this.sex = ‘undefined’;
      this.instances = this.instances || [];
      this.instances.push(this);
    }
    TaobaoUser.getAllInstances = function (){
      return this.instances;
    };
    TaobaoUser.prototype = new User;
    TaobaoUser.prototype.setSex = function (sex){
      this.sex = sex;
      return this;
    };
    TaobaoUser.prototype.constructor = TaobaoUser;

    var user = new TaobaoUser();
    user.setSex(‘female’).getSex();//female;
Alipay Inc

Calling method of SuperClass from SubClass

   User._getSelfSex_ = function (){
     return this.sex || ‘This is User’s sex.’;
   };

   TaobaoUser.getSex = function (){
     var superSex = User.prototype.getSex();
     return User._getSelfSex_() + ‘ ’ + this.sex;
   };

   var user = new TaobaoUser;
   user.getSex(); // This is User’s sex undefined;
Alipay Inc

  Object Oriented Programming Goals

   • Encapsulation
     实现类成员,方法的调用


   • Polymorphism
     实现在不同的类或对象中响应同样的方法或事件


   • Inheritance
     根据一个对象或类的行为来定义另一个对象或类的行为
Alipay Inc

Mais conteúdo relacionado

Mais procurados

Paying off technical debt with PHPSpec
Paying off technical debt with PHPSpecPaying off technical debt with PHPSpec
Paying off technical debt with PHPSpecLewis Wright
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptDonald Sipe
 
Crafting Quality PHP Applications (ConFoo YVR 2017)
Crafting Quality PHP Applications (ConFoo YVR 2017)Crafting Quality PHP Applications (ConFoo YVR 2017)
Crafting Quality PHP Applications (ConFoo YVR 2017)James Titcumb
 
Java ap is you should know
Java ap is you should knowJava ap is you should know
Java ap is you should knowHendrik Ebbers
 
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)James Titcumb
 
An Introduction to Ruby on Rails 20100506
An Introduction to Ruby on Rails 20100506An Introduction to Ruby on Rails 20100506
An Introduction to Ruby on Rails 20100506Vu Hung Nguyen
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to DomainJeremy Cook
 
An introduction-to-ruby-on-rails
An introduction-to-ruby-on-railsAn introduction-to-ruby-on-rails
An introduction-to-ruby-on-railsvinicorp
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Stephan Hochdörfer
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit TestingWen-Tien Chang
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first timeYusuke Kita
 
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)James Titcumb
 

Mais procurados (20)

Paying off technical debt with PHPSpec
Paying off technical debt with PHPSpecPaying off technical debt with PHPSpec
Paying off technical debt with PHPSpec
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Crafting Quality PHP Applications (ConFoo YVR 2017)
Crafting Quality PHP Applications (ConFoo YVR 2017)Crafting Quality PHP Applications (ConFoo YVR 2017)
Crafting Quality PHP Applications (ConFoo YVR 2017)
 
Java ap is you should know
Java ap is you should knowJava ap is you should know
Java ap is you should know
 
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
 
IoC&Laravel
IoC&LaravelIoC&Laravel
IoC&Laravel
 
Patterns In-Javascript
Patterns In-JavascriptPatterns In-Javascript
Patterns In-Javascript
 
Web application
Web applicationWeb application
Web application
 
Web application
Web applicationWeb application
Web application
 
Moneyandcurrency2
Moneyandcurrency2Moneyandcurrency2
Moneyandcurrency2
 
An Introduction to Ruby on Rails 20100506
An Introduction to Ruby on Rails 20100506An Introduction to Ruby on Rails 20100506
An Introduction to Ruby on Rails 20100506
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to Domain
 
An introduction-to-ruby-on-rails
An introduction-to-ruby-on-railsAn introduction-to-ruby-on-rails
An introduction-to-ruby-on-rails
 
ParisJS #10 : RequireJS
ParisJS #10 : RequireJSParisJS #10 : RequireJS
ParisJS #10 : RequireJS
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first time
 
Advance JS and oop
Advance JS and oopAdvance JS and oop
Advance JS and oop
 
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
 
slingmodels
slingmodelsslingmodels
slingmodels
 

Destaque

SASS 让你更爽的 编写CSS
SASS 让你更爽的 编写CSS SASS 让你更爽的 编写CSS
SASS 让你更爽的 编写CSS Zhu Qi
 
用户体验组设计流程
用户体验组设计流程用户体验组设计流程
用户体验组设计流程Zhu Qi
 
Web app share
Web app shareWeb app share
Web app shareZhu Qi
 
支付宝WAP站点收银台适配ios &android
支付宝WAP站点收银台适配ios &android支付宝WAP站点收银台适配ios &android
支付宝WAP站点收银台适配ios &androidZhu Qi
 
The core of javascript
The core of javascriptThe core of javascript
The core of javascriptspringuper
 
Building User-Centred Websites with Drupal
Building User-Centred Websites with DrupalBuilding User-Centred Websites with Drupal
Building User-Centred Websites with Drupalamanda etches
 
Sharing Those Resources - It\'s All About Access and Integration
Sharing Those Resources - It\'s All About Access and IntegrationSharing Those Resources - It\'s All About Access and Integration
Sharing Those Resources - It\'s All About Access and IntegrationMartha Rossi
 
Reference on the Go: Text Messaging and More
Reference on the Go: Text Messaging and MoreReference on the Go: Text Messaging and More
Reference on the Go: Text Messaging and MoreEllen Peterson
 
How I Built a Website for $16 in Chocolate
How I Built a Website for $16 in ChocolateHow I Built a Website for $16 in Chocolate
How I Built a Website for $16 in ChocolateLaura Crossett
 
Dallas Drupal Days 2012 - Introduction to less sass-compass
Dallas Drupal Days 2012  - Introduction to less sass-compassDallas Drupal Days 2012  - Introduction to less sass-compass
Dallas Drupal Days 2012 - Introduction to less sass-compassChris Lee
 
Assessing Information Literacy Online
Assessing Information Literacy OnlineAssessing Information Literacy Online
Assessing Information Literacy OnlineMargot
 

Destaque (11)

SASS 让你更爽的 编写CSS
SASS 让你更爽的 编写CSS SASS 让你更爽的 编写CSS
SASS 让你更爽的 编写CSS
 
用户体验组设计流程
用户体验组设计流程用户体验组设计流程
用户体验组设计流程
 
Web app share
Web app shareWeb app share
Web app share
 
支付宝WAP站点收银台适配ios &android
支付宝WAP站点收银台适配ios &android支付宝WAP站点收银台适配ios &android
支付宝WAP站点收银台适配ios &android
 
The core of javascript
The core of javascriptThe core of javascript
The core of javascript
 
Building User-Centred Websites with Drupal
Building User-Centred Websites with DrupalBuilding User-Centred Websites with Drupal
Building User-Centred Websites with Drupal
 
Sharing Those Resources - It\'s All About Access and Integration
Sharing Those Resources - It\'s All About Access and IntegrationSharing Those Resources - It\'s All About Access and Integration
Sharing Those Resources - It\'s All About Access and Integration
 
Reference on the Go: Text Messaging and More
Reference on the Go: Text Messaging and MoreReference on the Go: Text Messaging and More
Reference on the Go: Text Messaging and More
 
How I Built a Website for $16 in Chocolate
How I Built a Website for $16 in ChocolateHow I Built a Website for $16 in Chocolate
How I Built a Website for $16 in Chocolate
 
Dallas Drupal Days 2012 - Introduction to less sass-compass
Dallas Drupal Days 2012  - Introduction to less sass-compassDallas Drupal Days 2012  - Introduction to less sass-compass
Dallas Drupal Days 2012 - Introduction to less sass-compass
 
Assessing Information Literacy Online
Assessing Information Literacy OnlineAssessing Information Literacy Online
Assessing Information Literacy Online
 

Semelhante a Understand Prototype Inheritance in JavaScript

Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptForziatech
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfkatarichallenge
 
Javascript Styles and some tips
Javascript Styles and some tipsJavascript Styles and some tips
Javascript Styles and some tipsVítor Baptista
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript EverywherePascal Rettig
 
JavaScript In Object Oriented Way
JavaScript In Object Oriented WayJavaScript In Object Oriented Way
JavaScript In Object Oriented WayBorey Lim
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascriptguest4d57e6
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Expressjguerrero999
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp DeveloperSarvesh Kushwaha
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Fwdays
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developerSalvatore Fazio
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScriptYakov Fain
 
Intro to computer vision in .net update
Intro to computer vision in .net   updateIntro to computer vision in .net   update
Intro to computer vision in .net updateStephen Lorello
 

Semelhante a Understand Prototype Inheritance in JavaScript (20)

Object Oriented Programming In JavaScript
Object Oriented Programming In JavaScriptObject Oriented Programming In JavaScript
Object Oriented Programming In JavaScript
 
JavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdfJavaScript Interview Questions Part - 1.pdf
JavaScript Interview Questions Part - 1.pdf
 
Metaprogramming in ES6
Metaprogramming in ES6Metaprogramming in ES6
Metaprogramming in ES6
 
Javascript Styles and some tips
Javascript Styles and some tipsJavascript Styles and some tips
Javascript Styles and some tips
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Core concepts-javascript
Core concepts-javascriptCore concepts-javascript
Core concepts-javascript
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
JavaScript In Object Oriented Way
JavaScript In Object Oriented WayJavaScript In Object Oriented Way
JavaScript In Object Oriented Way
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascript
 
Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
 
JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
CiviCRM API v3
CiviCRM API v3CiviCRM API v3
CiviCRM API v3
 
Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
 
Spring data requery
Spring data requerySpring data requery
Spring data requery
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
JavaScript Essentials
JavaScript EssentialsJavaScript Essentials
JavaScript Essentials
 
Intro to computer vision in .net update
Intro to computer vision in .net   updateIntro to computer vision in .net   update
Intro to computer vision in .net update
 

Último

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Understand Prototype Inheritance in JavaScript

  • 1. Alipay Inc Understand Prototype 颂赞 (http://qiqicartoon.com)
  • 2. Alipay Inc • What is prototype? • How to inherit? • Simple Object • Defining a Class • Defining and Calling method in Class • Calling the SuperClass from SubClass • Overriding Method of SuperClass in SubClass • Calling Method of SuperClass from SubClass
  • 3. Alipay Inc What is prototype? By ECMASCRIPT: Each constructor is a function that has a property named ―prototype that is used to implement prototype-based inheritance and shared properties.
  • 4. Alipay Inc Prototype is prototype? Prototype contain: • Prototype property • Prototype object
  • 5. Alipay Inc What is prototype? function User(){ //… } Prototype property User.prototype = { //methods… }; Prototype object
  • 6. Alipay Inc What is prototype? Prototype object也是类成员的集合
  • 7. Alipay Inc What is prototype? Array, Object, Number, Function, Null, Undefind function _arrayObj(){} Array.prototype = new _arrayObj(); console.log(Array.prototype.constructor) ;//Array,not Function var userName = ‘zhuiq’, companies = [‘alipay’,’alisoft’], friends = { ‘a’:{},’b’:{} }; userName.prototype = {};//? companies.prototype = {};//? friends.prototype = {};//? null.prototype //? undefined.prototype //?
  • 8. Alipay Inc What is prototype? Javascript原始对象的prototype是只读的! 只有函数对象才有prototype属性!
  • 9. Alipay Inc What is prototype? methods • __defineGetter__ • __defineSetter__ • eval • hasOwnProperty • isPrototypeOf • valueOf Prototype • watch Object • unwatch • toString properties • toSource • propertyIsEnumerable • constructor • __noSuchMethod__ • __parent__ • __lookupGetter__ • __proto__ • __lookupSetter__ • __count__
  • 10. Alipay Inc How to inherit? PHP: extends JAVA: extends Ruby: < ASP: : Python: class subClassName(SuperClassName) C++: : Javascript: prototype
  • 11. Alipay Inc How to inherit?
  • 12. Alipay Inc How to inherit? var CF= function (){}, CFp = {CFP1:1,CFP2:2}; CF.prototype = CFp; CF.prototype.P1 = ‘P1’; CF.prototype.P2 = ‘P2’; var cf1 = new CF, cf2 = new CF, cf3 = new CF, cf4 = new CF, cf5 = new CF;
  • 13. Alipay Inc Simple Object function User(){ this.firstName = ‘颂’; this.lastName = ‘赞’; } var user = {firstName:’zhu’,lastName:’qi’}; User.prototype = user; var userByConstructor = new User(); user userByConstructor firstName zhu firstName 颂 lastName qi lastName 赞 prototypeObject? [Object] prototypeObject:user prototype prototype constructor:Object constructor:Function
  • 14. Alipay Inc function User(){ this.sex= ‘male’; } User.prototype = { getSex : function (){return this.sex;} }; function AlipayUser(){ this.sex = ‘female’; } function TaobaoUser(){} AlipayUser.prototype = new User; TaobaoUser.prototype = new User; AlipayUser.prototype.constructor = AlipayUser; TaobaoUser.prototype.constructor = TaobaoUser; var alipay_user = new AlipayUser (); var taobao_user = new TaobaoUser;
  • 15. Alipay Inc User sex male prototypeObject: Object prototype constructor:Function TaobaoUser sex male prototypeObject: Object AlipayUser prototype sex female constructor:TaobaoUser prototypeObject: Object prototype constructor:AlipayUser
  • 16. Alipay Inc console.log(alipay_user .constructor);//AlipayUser console.log(taobao_user .constructor);//TaobaoUser console.log(alipay_user instanceof AlipayUser );//true; console.log(taobao_user instanceof TaobaoUser);//true; console.log( taobao_user instanceof User && alipay_user instanceof User );//true
  • 17. Alipay Inc Defining and Calling method in Class function TaobaoUser(cfg){ User.call(this,cfg); this.sex = ‘undefined’; this.instances = this.instances || []; this.instances.push(this); } TaobaoUser.getAllInstances = function (){ return this.instances; }; TaobaoUser.prototype = new User; TaobaoUser.prototype.setSex = function (sex){ this.sex = sex; return this; }; TaobaoUser.prototype.constructor = TaobaoUser; var user = new TaobaoUser(); user.setSex(‘female’).getSex();//female;
  • 18. Alipay Inc Calling method of SuperClass from SubClass User._getSelfSex_ = function (){ return this.sex || ‘This is User’s sex.’; }; TaobaoUser.getSex = function (){ var superSex = User.prototype.getSex(); return User._getSelfSex_() + ‘ ’ + this.sex; }; var user = new TaobaoUser; user.getSex(); // This is User’s sex undefined;
  • 19. Alipay Inc Object Oriented Programming Goals • Encapsulation 实现类成员,方法的调用 • Polymorphism 实现在不同的类或对象中响应同样的方法或事件 • Inheritance 根据一个对象或类的行为来定义另一个对象或类的行为