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

  • SEARCH
  • Home
  • 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 7852447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:25:13+00:00 2026-06-02T19:25:13+00:00

I have a method that will let me select the prototype object when creating

  • 0

I have a method that will let me select the prototype object when creating a new object (copied from “Javascript: The Good Parts” book):

Object.create = function(o) {
    var F = function() {};
    F.prototype=o;
    return new F();
}

Now say, I have an object:

var car = {
   model: "Nissan"
};

And I create a new object based on this object, using the “Create” method:

var car1 = Object.create(car);

I can then add a property to car and it will dynamically get added to car1 (dynamic prototyping). So for eg:

car.year=2011;      // Gets added to "car"...
alert(car1.year);   // ... Is also avaialable to car1

Q1) This behavior indicates that “year” got added to car’s prototype, which is why it is available to car1. Is this correct? If not, then where does “year” get added and why is it available to both “car” and “car1”?

Also, per the rule of delegation, if a method cannot be found on an object, it will search its prototype and then check all the prototypes up the chain till it gets to Object.prototype. So now, if I type something like this:

Object.prototype.originCountry = "Japan";
alert(car.originCountry);   // Outputs Japan
alert(car1.originCountry);  // Outputs Japan

So far so good; however, if I do:

Object.carColor= "White";
alert(car.carColor);   // Error!

Q2) When I add a property to “car” (see car.year example above, it gets added to car’s prototype. However, when I add a property to Object, it does not get added to Object’s prototype? If it does get added to Object’s prototype, then why is it not available to “car”, per the rule of delegation?

Why is this happening?

  • 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-06-02T19:25:14+00:00Added an answer on June 2, 2026 at 7:25 pm

    When you do this:

    Object.carColor = "White";
    

    Then the property carColor does not get added to the Object‘s prototype. It is now a property of Object. To see what you expect, what you would do is:

    Object.prototype.carColor = "White";
    

    Then after that:

    alert(({}).carColor); // Will alert "White"
    

    So what happens here is that. any object created including {} (which is nothing but an empty object) is a new instance of Object and hence shares the properties of whatever is set in the prototype of Object.

    As for how your Object.create function works. Let us look at it line-by-line:

     1. var F = function() {};
    

    You just create a new function, an essentially blank object. The reason you use a function and not something like {} is because a function can be coupled with a new call to create a new instance of that object wherein the function would act as a constructor.

    2. F.prototype=o;
    

    You set the prototype of the new blank function to the object you’ve created. Now, this is purely a reference. It is not a deep-copy. What I mean is that as the object o changes, so will any instances of the objects (actually they won’t change, but they would ‘seem to’ change. More on that later).

    3. return new F();
    

    Now you just create a new instance of that function, which has a prototype as the object you passed.

    When you do the following:

    var car1 = Object.create(car);
    

    You get an object car1 which has the prototype has car. So when you do this:

    car.year = 2011
    

    It isn’t like car1 changes. It is more like the object that the prototype refers to changes. So when you do something like:

    car1.year
    

    A search is made (first in the prototype, then in the object) for a property called year and turns out, that the prototype has it and hence car1.year will return 2011.

    So the bottom line is this:

    1. A prototype is shared amongst instances.
    2. Changing the properties of an Object will not manifest into any instances changing.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a class that will have one public method, which returns a value
I have a method that will receive a string , but before I can
I have a method with this signature, and another method that will be the
I have a method that I will use in the following contexts: 1. User
I have some code in method C that will get executed based upon the
I'm trying to write a method that will return a Hibernate object based on
I have setup a delegate method, so that when user selects cell from VC2,
I’m after some C# code that will have the following methods that will return
I have a web application that will need to access code behind methods as
I have a method that accepts an IEnumerable-decimals and performance various math functions. I

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.