Maison > interface Web > js tutoriel > le corps du texte

Fils brisés, prototypes et chaînes de prototypes en JavaScript

灭绝师太
Libérer: 2021-11-04 12:00:01
original
1319 Les gens l'ont consulté

Fils brisés, prototypes et chaînes de prototypes en JavaScript

La relation entre le prototype et la chaîne de prototypes traverse les objets en JavaScript, et tout dans JavaScript est un objet, donc le prototype et la chaîne de prototypes sont des concepts relativement importants. Apportons-le aujourd'hui. Jetons un coup d'œil au prototype et à la chaîne de prototypes en JavaScript. JavaScript中的对象,而JavaScript中万物皆对象,所以原型和原型链是比较重要的概念,今天就带大家一起来看一看JavaScript中的原型与原型链。

一、了解概念(知道有这两个名词即可)

  1. 原型(<em>prototype</em>

  2. 原型链(__<em>proto__</em>

二、了解从属关系

prototype  => 函数的一个属性         : 同时也是一个 对象{} (称之为原型对象亦可)

__proto__ => 对象Object的一个属性   : 同时也是一个 对象{}   (__proto__也就是[[Prototype]])
Copier après la connexion

注:对象的__proto__保存着该对象的构造函数的prototype

a.声明一个构造函数

    function Test() { }
  
    //prototype 是函数的一个属性
    console.dir(Test);
    console.log(Test.prototype);
    // Test.prototype也是一个对象
    console.log(typeof Test.prototype);
Copier après la connexion

b.声明一个对象

    const test = new Test();
    
    console.log(test);
    //验证test为一个对象
    console.log(typeof test);
    //__proto__是对象的一个属性
    console.log(test.__proto__);
    console.log(Test.prototype);
    //对象的__proto__属性存储着Test.prototype
    console.log(test.__proto__ === Test.prototype);
    // test.__proto__也是一个对象
    console.log(typeof test.__proto__);
Copier après la connexion
function Test() {}
console.log(Test.prototype); //验证函数是否有prototype属性

let test = new Test();
console.dir(test.__proto__); //验证对象是否有__proto__属性
console.log(test.__proto__ === Test.prototype);//验证对象的__ptoto__是否保存着该对象的构造函数的prototype

console.log(Test.prototype.__proto__ === Object.prototype);//Test.prototype(是一个对象)的__proto__属性是否是对象的原型属性
console.log(Object.prototype.__proto__);//原型链的顶层没有__proto__属性 null
Copier après la connexion

三、深入认识原型链、原型和原型继承

    function Test(){}
    let test =new Test();
    test.a= 10;
    //test.__proto__ === test.constructor.prototype
    test.__proto__.b1=11;//对象的__proto__属性存储着对象构造函数的prototype属性
    Test.prototype.b2=11;
   
    test.__proto__.__proto__.c1=12;
    Object.prototype.c2=12;
    console.log(test);
    console.log(Test.prototype);
    console.log(Object.prototype.__proto__);
   
    /*逐层解析
    * test{
    *        a = 10
    *        __proto__:Test.prototype{
    *                 b = 11
    *                 __proto__:Object.prototype{
    *                           c = 12
    *                           X__prototype__:null
    *                 }         
    *        }
    *     }
    *
    * */
Copier après la connexion

四、总结

  • 不建议直接用 __proto__ 访问。

  • 可以简单概括为以<span style="color: rgb(0, 0, 0);">prototype</span>为原型节点, <span style="color: rgb(0, 0, 0);">__proto__</span>为原型链条。

  • 每个实例对象(<span style="color: rgb(0, 0, 0);">object</span>)都有一个私有属性(称之为 <span style="color: rgb(0, 0, 0);">__proto__</span> )指向它的构造函数的原型对象(<span style="color: rgb(0, 0, 0);">prototype</span>)。该原型对象也有一个自己的原型对象(<span style="color: rgb(0, 0, 0);">__proto__</span>),层层向上直到一个对象的原型对象为 <span style="color: rgb(0, 0, 0);">null</span>。根据定义,<span style="color: rgb(0, 0, 0);">null</span> 没有原型,并作为这个原型链中的最后一个环节。

  •  <span style="color: rgb(0, 0, 0);">someObject.[[Prototype]]</span> 符号是用于指向 <span style="color: rgb(0, 0, 0);">someObject</span> 的原型,<span style="color: rgb(0, 0, 0);">someObject.[[Prototype]]</span> === <span style="color: rgb(0, 0, 0);">__proto__</span>(JavaScript的非标准但许多浏览器实现的属性)。

  • <span style="color: rgb(0, 0, 0);">Object.prototype</span> 属性表示 <span style="color: rgb(0, 0, 0);">Object</span> 的原型对象。

  •  被构造函数创建的实例对象的 <span style="color: rgb(0, 0, 0);">[[Prototype]]</span> 指向 <span style="color: rgb(0, 0, 0);">func</span><span style="color: rgb(0, 0, 0);">prototype</span>

    1. Comprendre les concepts (il suffit de connaître ces deux termes)
  1. Prototype (<em>prototype</em>)

  2. Chaîne de prototypes (__<em>proto__</em>)

2. Comprendre les affiliations🎜rrreee🎜Remarque : le __proto__ de l'objet enregistre le prototype du constructeur de l'objet🎜🎜 a . Déclarez un constructeur
🎜rrreee🎜 b Déclarez un objet🎜rrreeerrreee🎜🎜rrreee🎜Résumé
🎜
  • 🎜Il n'est pas recommandé d'utiliser __proto__ Accès. 🎜🎜
  • 🎜 peut être simplement résumé en prenant <span style="color: rgb(0, 0, 0);">prototype</span> comme nœud prototype, <span style="color: rgb(0, 0, 0);">__proto__</span> est la chaîne de prototypes. 🎜🎜
  • 🎜Chaque objet d'instance (<span style="color: rgb(0, 0, 0);">objet</span>) possède une propriété privée (appelée For <span style="color: rgb(0, 0, 0);">__proto__</span> ) pointe vers l'objet prototype de son constructeur (<span style="color : rgb( 0, 0, 0);">prototype</span>). L'objet prototype possède également son propre objet prototype (<span style="color: rgb(0, 0, 0);">__proto__</span>), jusqu'au prototype d'un object L'objet est <span style="color: rgb(0, 0, 0);">null</span>. Par définition, <span style="color: rgb(0, 0, 0);">null</span> n'a pas de prototype et est le dernier maillon de cette chaîne de prototypes. 🎜🎜
  • 🎜 <span style="color: rgb(0, 0, 0);">quelqueObjet.[[Prototype ]]</span> le symbole est utilisé pour pointer vers <span style="color: rgb(0, 0, 0);">someObject</span> Prototype,<span style="color: rgb(0, 0, 0);">someObject.[[Prototype]]</span> === <span style="color: rgb(0, 0, 0);">__proto__</span>(JavaScript non standard mais de nombreuses propriétés implémentées dans le navigateur). 🎜🎜
  • 🎜<span style="color: rgb(0, 0, 0);">Object.prototype</span> représente l'objet prototype de <span style="color: rgb(0, 0, 0);">Object</span>. 🎜🎜
  • 🎜 Le <span style="color: rgb(0, 0, 0);">[[Prototype]]</span> de l'objet instance créé par les points constructeur à<span style="color: rgb(0, 0, 0);">func</span>'s<span style="color: rgb(0, 0, 0 );">prototype</span>. 🎜🎜🎜🎜Vidéos recommandées : 🎜"Introduction rapide à la série JavaScript_Jade Girl Heart Sutra"🎜, 🎜"Dugu Jiujian (6)_jQuery Video Tutorial"🎜🎜

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!