Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7129871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:17:21+00:00 2026-05-28T11:17:21+00:00

Consider the following code: function Robot(weapon) { this.weapon = weapon; this.fire = function() {

  • 0

Consider the following code:

function Robot(weapon) {
    this.weapon = weapon;
    this.fire = function() {
        alert('Firing ' + this.weapon + 'projectile.');
    };
}

var gunRobot = new Robot('gun');

vs.

function makeRobot(weapon) {
    return {
        weapon: weapon,
        fire: function() {
            alert('Firing ' + this.weapon + 'projectile.');
        }
    };
}

var laserRobot = makeRobot('laser');

So, if I understood right, the main difference between these two is that in the first example, gunRobot has a constructor property which points to Robot, whereas in the second example, laserRobot has a construtor property which points to Object. Both don’t add anything to their prototype, right?

Now, if I change the first example to:

function Robot(weapon) {
    this.weapon = weapon;
}

Robot.prototype.fire = function() {
        alert('Firing ' + this.weapon + 'projectile.');
};

var gunRobot = new Robot('gun');

gunRobot’s constructor property points to the Robot constructor, whose prototype property has a property fire. Now, if I call

gunRobot.fire();

as, gunRobot doesn not have a fire property, the next prototype up in the prototype chain is visited and checked for a fire property, right?

I’m mainly a Java programmer, so it’s quite hard to wrap my head around JS notion of object-orientation :-).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:17:22+00:00Added an answer on May 28, 2026 at 11:17 am

    Really, really close, but considering this sentence…

    “gunRobot’s constructor property points to the Robot constructor, whose prototype property has a property fire.”

    …it seems that you may think that it is via the constructor property that gunRobot will access its prototype object.

    That wouldn’t be correct. The gunRobot object doesn’t have a property of its own called constructor, but that property is actually a member of the prototype object itself.

    The way that the prototype object is referenced is internal and therefore implicit.

    This means…

    • You could shadow the constructor property on the gunRobot object, and still have your implicit reference to the prototype object

    • You could delete the constructor property from the prototype object, and the prototype chain would be unaffected

    • If you entirely replace the prototype object of the constructor, any objects that have already been created from the constructor do not see the change. Their prototype object reference is permanent. Only new objects would have the internal reference to the new prototype object.


    And with respect to this question…

    “Both don’t add anything to their prototype, right?”

    …you’re right that nothing has been added to the prototype object of either object. (Of course, the default Object.prototype object has some default properties.)


    • gunRobot —-> Robot.prototype (empty object)

    • Robot.prototype —-> Object.prototype

    • Object.prototype —-> null


    • laserRobot —-> Object.prototype

    • Object.prototype —-> null

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: $(.lcontainer a).click(function () { var current = $(this); var name
consider the following code: <script> function testFunc(){ alert('test') } $(function(){ var g = document.getElementById
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following code example: TempList.ForEach(Function(obj) obj.Deleted = True End Function) And this one:
Consider the following code: var Widget = new Class({ Implements: [Options], options: { name
Consider the following code: MyClass.prototype.my_func = function () { this.x = 10; $.ajax({ //
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
Please consider the following code (also in this fiddle ): var viewModel = {
Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.