Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. What's the difference between "prototype" and "__proto__"?

What's the difference between "prototype" and "__proto__"?

Scheduled Pinned Locked Moved JavaScript
javascriptquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yumeng liu
    wrote on last edited by
    #1

    What's the difference between "prototype" and "__proto__" in JavaScript?

    L S 2 Replies Last reply
    0
    • Y yumeng liu

      What's the difference between "prototype" and "__proto__" in JavaScript?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Something like this[^].

      speaking as ...

      1 Reply Last reply
      0
      • Y yumeng liu

        What's the difference between "prototype" and "__proto__" in JavaScript?

        S Offline
        S Offline
        Sherin_Mathew
        wrote on last edited by
        #3

        __proto__ - __proto__ is the actual object that is used in the lookup chain to resolve methods. - It is a property that all objects have. This is the property which is used by the JavaScript engine for inheritance. - According to ECMA specifications it is supposed to be an internal property, however most vendors allow it to be accessed and modified. Syntax

        var Circle = function () {};
        var shape = {};
        var circle = new Circle();

        // Set the object prototype.
        // DEPRECATED.This is for example purposes only. DO NOT DO THIS in real code.
        shape.__proto__ = circle;

        // Get the object prototype
        console.log(shape.__proto__ === circle); // true

        prototype - prototype is a property belonging only to functions. - It is used to build __proto__ when the function happens to be used as a constructor with the new keyword. - In prototype-based object oriented languages like Self and Javascript, every object in the system has a field that says "if I don't have a property or method that is requested of me, go to the object that this field references my prototype and look for it". - Since that object will also have this "prototype" field as well, this becomes a recursive process. - It is what is meant by a prototype chain. - Note that this means that in a prototype language, there is no abstract concept of a "class" Syntax

        var shape = function () {};
        var p = {
        a: function () {
        console.log('aaa');
        }
        };
        shape.prototype.__proto__ = p;

        var circle = new shape();
        circle.a(); // aaa
        console.log(shape.prototype === circle.__proto__); // true

        // or
        var shape = function () {};
        var p = {
        a: function () {
        console.log('a');
        }
        };

        var circle = new shape();
        circle.__proto__ = p;
        circle.a(); // a
        console.log(shape.prototype === circle.__proto__); // false

        // or
        function test() {};
        test.prototype.myname = function () {
        console.log('myname');
        };

        var a = new test();
        console.log(a.__proto__ === test.prototype); // true
        a.myname(); // myname

        // or
        var fn = function () {};
        fn.prototype.myname = function () {
        console.log('myname');
        };

        var obj = {
        __proto__: fn.prototype
        };

        obj.myname(); // myname

        L 1 Reply Last reply
        0
        • S Sherin_Mathew

          __proto__ - __proto__ is the actual object that is used in the lookup chain to resolve methods. - It is a property that all objects have. This is the property which is used by the JavaScript engine for inheritance. - According to ECMA specifications it is supposed to be an internal property, however most vendors allow it to be accessed and modified. Syntax

          var Circle = function () {};
          var shape = {};
          var circle = new Circle();

          // Set the object prototype.
          // DEPRECATED.This is for example purposes only. DO NOT DO THIS in real code.
          shape.__proto__ = circle;

          // Get the object prototype
          console.log(shape.__proto__ === circle); // true

          prototype - prototype is a property belonging only to functions. - It is used to build __proto__ when the function happens to be used as a constructor with the new keyword. - In prototype-based object oriented languages like Self and Javascript, every object in the system has a field that says "if I don't have a property or method that is requested of me, go to the object that this field references my prototype and look for it". - Since that object will also have this "prototype" field as well, this becomes a recursive process. - It is what is meant by a prototype chain. - Note that this means that in a prototype language, there is no abstract concept of a "class" Syntax

          var shape = function () {};
          var p = {
          a: function () {
          console.log('aaa');
          }
          };
          shape.prototype.__proto__ = p;

          var circle = new shape();
          circle.a(); // aaa
          console.log(shape.prototype === circle.__proto__); // true

          // or
          var shape = function () {};
          var p = {
          a: function () {
          console.log('a');
          }
          };

          var circle = new shape();
          circle.__proto__ = p;
          circle.a(); // a
          console.log(shape.prototype === circle.__proto__); // false

          // or
          function test() {};
          test.prototype.myname = function () {
          console.log('myname');
          };

          var a = new test();
          console.log(a.__proto__ === test.prototype); // true
          a.myname(); // myname

          // or
          var fn = function () {};
          fn.prototype.myname = function () {
          console.log('myname');
          };

          var obj = {
          __proto__: fn.prototype
          };

          obj.myname(); // myname

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Very interesting, but that question was posted nearly eight years ago. It is most unlikely that the poster is still working on the same issues. Focusing on current questions is a better idea.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups