We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Avatar
Join the discussion…


  • in this conversation
      Media preview placeholder
      Log in with
      or sign up with Disqus or pick a name

      Disqus is a discussion network

      • Disqus never moderates or censors. The rules on this community are its own.
      • Your email is safe with us. It's only used for moderation and optional notifications.
      • Don't be a jerk or do anything illegal. Everything is easier that way.

      Read full terms and conditions

      By signing up, you agree to the Disqus Basic Rules, Terms of Service, and Privacy Policy.
      By posting, you agree to the Disqus Basic Rules, Terms of Service, and Privacy Policy.
      • Thank you for your clear and thorough summary. It's very helpful.

        One suggestion about the Prototype Chain example. It would be more clear if use a different object to assign the prototype to the empty constructor other than "Guy". For me I was confused at the first time that why we need to assign the "Guy.prototype" if we have "Guy.apply" since "Guy" already have the aboutMe(). I commented out the 3 lines for Guy.prototype assignment, and it is still working.

        Here is the testing code I use to understand the concept (I am using NGuy for the prototype assignment and Guy for constructor):

        function Guy(name, awesome) {

        this.name = name;

        this.awesome = awesome;

        }

        function NGuy(name, awesome) {

        this.name = name;

        this.awesome = awesome;

        //this.aboutMe = function() {

        //return this.awesome ? "I'm awesome!" : "I'm lame :(";

        //};

        }

        NGuy.prototype.aboutMe = function() {

        return this.awesome ? "I'm awesome!" : "I'm lame :(";

        };

        //var guy1= new NGuy("Max",true);

        //console.log(guy1.aboutMe());

        function NewGuy() {

        Guy.apply(this, arguments);

        }

        var ctor = function() {}; // empty constructor

        ctor.prototype = NGuy.prototype;

        NewGuy.prototype = new ctor();

        NewGuy.prototype.shout = function() {

        return "Aaaaaaahhhh";

        }

        var john = new NewGuy("New John", true);

        console.log(john.aboutMe());

        console.log(john.shout());

          see more
          • thanks for that, very helpful

              • Hopefully this gets updated with ES6 class syntax.

                  • Thank you for this amazing summary on how create objects in JavaScript!

                      • One of the better articles I've read on this subject, JavaScript rocks!

                        • Very helpful-thanks!

                            • Great stuff! It really helped me clear some doubts about constructor functions