Object oriented programming (2)

Want to fly ash 2021-09-15 08:22:14
object oriented programming


In the last article, I knew how to use object Or object literal to facilitate the creation of objects , But there are still some shortcomings , This causes multiple objects that create the same interface to need to be defined repeatedly , Poor reuse rate of code , So in ES6 Formal support for classes and inheritance .

Factory mode

function person(name,sex,year){
   let o=new Object();
   let o.name=name;
   let o.sex=sex;
   let o.year=year;
   let o.selfDescriptor=function(){
    console.log(this.name)  
  }
   return o;
}
let person1=person('yangyang',' male ',24);
console.log(person1);
//{
// name: 'yangyang',
// sex: ' male ',       
//year: 24,
// selfDescriptor: [Function (anonymous)]
//}
 Copy code 

Although factory pattern can create similar objects , But it doesn't solve the problem of object identification

Constructor Pattern

function Person(name,sex,age){
this.name=name;
this.sex=sex;
this.age=age;
this.sayname=function(){
​
console.log(this.name);
}
}
​
let person1=new Person('fangfua',' Woman ',33);
let person1=new Person('lulu',' male ',21);
person1.sayname();//fangfua
person2.sayname();//lulu
 Copy code 

Different from the factory model

  • Create objects that are not shown
  • Properties and methods are assigned directly to this
  • No, return
  • Function names need to be capitalized

new What happens inside the operator when it creates an instance

  1. A new object is created in memory

  2. Inside this new object [[Prototype]] Property is assigned to the constructor prototype attribute

  3. In the constructor this Assigned to this new object

  4. Code to execute the constructor , Add properties to the new object

  5. If the constructor returns a non empty object , Returns the object , otherwise , Return the newly created object

    When constructing instantiation, if no parameters are passed , The parentheses after the constructor can be deleted

Archetypal model

Using prototype objects , The properties and methods defined above can be shared by instances ;

function Person(){
Person.prototype.name="fangfua";
Person.prototype.sex=' Woman ';
Person.prototype.age=33;
Person.prototype.sayname=function(){
​
console.log(this.name);
}
}
​
let person1=new Person();
let person1=new Person();
person1.sayname();//fangfua
person2.sayname();//fangfua
 Copy code 

Just create a function , Will follow the rules , Create a... For the function prototype Attribute points to prototype object , The prototype object will also have a constructor Properties of , Point to the constructor associated with it ;

Every time you create an instance , Constructor's [[prototype]] Property will be assigned to the instance object , Namely proto

Be careful : Instances are directly related to prototypes , Instances and constructors do not

How to judge that the instance object and constructor have the same prototype object

Prototype .isPrototypeOf( example )

Example :Person.prototype.isPrototypeOf(person1)//true

Object.getPrototypeOf() Returns the internal properties of the parameter [[prototype]] Value

Prototype level

function Person(){
Person.prototype.name="fangfua";
Person.prototype.sex=' Woman ';
Person.prototype.age=33;
Person.prototype.sayname=function(){
​
console.log(this.name);
}
}
​
let person1=new Person();
let person1=new Person();
​
person1.name='gungun';
console.log(person1.name);//gungun From instance
console.log(person2.name);//fangfua From the prototype
​
​
 Copy code 

Find a property on the instance object , It will look up from the instance itself , If you don't find it, you'll find it from the prototype object

Object.hasOwnProperty() Method can determine whether the attribute appears on the instance or prototype (true Yes, on the example ,false It's on the prototype )

There are three methods to traverse the property name or value on the instance or prototype

for...in

Object.keys()

function Person(){
Person.prototype.name="fangfua";
Person.prototype.sex=' Woman ';
Person.prototype.age=33;
Person.prototype.sayname=function(){
​
console.log(this.name);
}
}
​
let keys=Object.keys(Person.prototype);
console.log(keys)//name,sex,age.sayname
 Copy code 

Object.values()

const o={
foo:'bar',
baz:1,
qux:{}
}
​
console.log(Object.values(o));//['bar',1,{}]
 Copy code 
版权声明
本文为[Want to fly ash]所创,转载请带上原文链接,感谢
https://javamana.com/2021/09/20210909135431064v.html

  1. Spring中@within与@target的一些区别
  2. 力荐:提高千倍效率的一些 Java 代码小技巧
  3. Redis技术专题系列之帮你从底层彻底吃透RDB技术原理(基础篇)
  4. Juan Benet et vitalik buterin discutent des réflexions sur les médias sociaux décentralisés
  5. Ipfs Weekly Report 152 | pinata launched "submarining"
  6. Performance optimization issue 03 - HTTP request optimization
  7. JavaScript genrator generator
  8. 字节跳动Java面试全套真题解析在互联网火了,面试大厂应该注意哪些问题?
  9. 字节跳动Java社招,2021年阿里 腾讯 快手offer都已拿到!
  10. 用Java实现红黑树
  11. 使用Redis Stream来做消息队列和在Asp.Net Core中的实现
  12. 海量列式非关系数据库HBase 架构,shell与API
  13. Redis Technology Topic Series vous aide à comprendre les principes de la technologie rdb du Bas (Basic)
  14. Conseils: quelques conseils pour améliorer l'efficacité du Code Java
  15. Quelques différences entre @ within et @ Target au printemps
  16. 海量列式非關系數據庫HBase 架構,shell與API
  17. Architecture, Shell et API de base de données non relationnelle à grande échelle
  18. Mise en œuvre de l'arbre Rouge et noir en Java
  19. Byte Hopping Java Service Call, 2021 Alibaba Tencent Express offer a été obtenu!
  20. Byte Jump Java interview Full Set of true Problems Analysis in Internet fire, interview Factory should pay attention to what Problems?
  21. La dernière réponse à l'entrevue de développement Android, l'hiver froid de l'industrie
  22. A young Lexus, the new NX refuses to be mediocre and mature
  23. Interprétation approfondie de l'équipe sin7y: application de plookup dans la conception de zkevm
  24. Java basic knowledge point Combing, redis Common Data Structures and Using scenario Analysis,
  25. Five minutes to understand MySQL index push down
  26. Data structure and algorithm (XI) -- algorithm recursion
  27. Programmation asynchrone Java scirp, développement frontal de base
  28. Java basic knowledge point video, three sides ant Gold Clothing successfully obtained offer,
  29. Oracle Linux bascule le noyau uek vers le noyau rhck pour résoudre les problèmes de compatibilité acfs
  30. After the grand finale of spring in jade mansion, after reading many comments, I began to sympathize with white deer
  31. 字节跳动Java高级工程师,统一命名服务、集群管理、分布式应用?
  32. 字节跳动Java高级工程师,深入分布式缓存从原理到实践技术分享,
  33. 字节跳动第三轮技术面,阿里P8架构师Java大厂面试题总结,
  34. 字节跳动社招Java面试,超通俗解析CountDownLatch用法和源码,
  35. 字节跳动最新开源,最经典的HashMap图文详解,
  36. 字節跳動第三輪技術面,阿裏P8架構師Java大廠面試題總結,
  37. Byte Jumping the Third Third Technical surface, Ali P8 Architect Java Factory Interview Question summary,
  38. L'ingénieur Java senior de Byte Hopping approfondit la mise en cache distribuée, du principe au partage de la technologie pratique.
  39. Byte Jump Java Senior Engineer, Unified Naming service, Cluster Management, Distributed application?
  40. Plusieurs méthodes de transfert de fichiers entre Windows et Linux
  41. 快速从 Windows 切换到 Linux 环境
  42. 五分钟向MySql数据库插入一千万条数据
  43. Java日期时间API系列42-----一种高效的中文日期格式化和解析方法
  44. 用Java实现红黑树
  45. 使用Redis Stream来做消息队列和在Asp.Net Core中的实现
  46. 海量列式非关系数据库HBase 架构,shell与API
  47. Architecture, Shell et API de base de données non relationnelle à grande échelle
  48. Mise en œuvre de l'arbre Rouge et noir en Java
  49. Java Date Time API Series 42 - - a efficient Chinese Date Format and Analysis Method
  50. 5 minutes pour insérer 10 millions de données dans la base de données MySQL
  51. Passage rapide de Windows à l'environnement Linux
  52. Notes on Java backend development of PostgreSQL (I)
  53. 海量列式非關系數據庫HBase 架構,shell與API
  54. Byte Jump the latest open source, the most Classic hashtap Graph details,
  55. L'interview Java de Byte Hopping Society, l'analyse super populaire de l'utilisation et du code source de countdownlatch,
  56. "Anti Mafia storm" Wang Zhifei's love history is really wonderful: he divorced Zhang Xinyi and married a 14-year-old wife
  57. In spring in the jade mansion, Jia Fengyuan was not moved by his brother's death. Why was su Yingxue changed? The reason is realistic
  58. Adam Oracle Oracle fully constructs Adam token incentive for ecological development
  59. 实战SpringCloud通用请求字段拦截处理,超过500人面试阿里,
  60. 宅家36天咸鱼翻身入职腾讯,Zookeeper一致性级别分析,