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 8494873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:25:00+00:00 2026-06-10T23:25:00+00:00

I have a question about the following canonical object.create method: Object.create = function(o, props)

  • 0

I have a question about the following canonical object.create method:

Object.create = function(o, props) {
    function F() {}
    F.prototype = o;

    if (typeof(props) === "object") {
     for (prop in props) {
      if (props.hasOwnProperty((prop))) {
       F[prop] = props[prop];
      }
     }
    }
    return new F();
   };

On line 3 of the above code we set the prototype property of the F object to the o argument’s prototype.

I would have thought this meant that both o and F point to the same prototype and therefore point to the same set of members.

But the code then goes onto copy all the members in the prop in props loop.

What is the point of setting the prototype in line 3 if we then go onto copy all the members manually?

  • 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-10T23:25:01+00:00Added an answer on June 10, 2026 at 11:25 pm

    There’s a mistake in the version of Object.create in your question: The loop attaches the properties to the constructor function F (not to the returned object, or its prototype) which means they’re not accessible in the created object.

    The properties of the second parameter to Object.create are supposed to be copied to the newly created object. The Mozilla documentation for Object.create puts it like this:

    If specified and not undefined, an object whose enumerable own properties (that is, those properties defined upon itself and not enumerable properties along its prototype chain) specify property descriptors to be added to the newly-created object, with the corresponding property names.

    Try running the following code with the version of Object.create in the question:

    o = Object.create(
            {a: "prototype's a", b: "prototype's b"},
            {a: "object's a"}
        );
    

    You’ll find that o.a == "prototype's a" and o.b == "prototype's b", "object's a" is lost.

    The following version of the function would probably be more useful:

    Object.create = function(o, props) {
        var newObj;
    
        // Create a constructor function, using o as the prototype
        function F() {}
        F.prototype = o;
    
        // Create a new object using F as the constructor function
        newObj = new F();
    
        // Attach the properties of props to the new object
        if (typeof(props) === "object") {
            for (prop in props) {
                if (props.hasOwnProperty((prop))) {
                    newObj[prop] = props[prop];
                }
            }
        }
    
        return newObj;
    };
    

    Let’s try it out with the same example:

    o = Object.create(
            {a: "prototype's a", b: "prototype's b"},
            {a: "object's a"}
        );
    

    The new object o is created with a prototype that has properties a and b and it’s own property a.

    Let’s look at o.b first: o.hasOwnProperty("b") will return false, because o does not have a property called b. That’s where the prototype comes in; because there is no property b it is looked up on the prototype, and therefore o.b === "prototype's b".

    On the other hand, o.hasOwnProperty("a") will return true, because o does have an a property. o.a == "object's a" and nothing is looked up from the prototype.

    As pointed out in @chuckj’s answer, the correct implementation of Object.create is more complicated than this. For more details, see John Resig’s post on ECMAScript 5 Objects and Properties.

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

Sidebar

Related Questions

I have a question about type casting. I have the following JSON String: {server:clients,method:whoIs,arguments:[hello]}
I have (another) question about indexing. I use the following code: CREATE TABLE [dbo].[PnrDetails1](
I have a question about the following code private void printTree(Node node){ if(node==null) return;
I have this question about c# language's dynamic binding behavior. Consider the following object
i have a question about the function call in the following example: int main()
I have a question about the following regular expression: I want to match the
I have a question about the following code in TCL/EXPECT: expect { timeout {
I have a question about the following C code: void my_function() { int i1;
I have a question about the following code : #include <iostream> #include <ctime> int
I have the following question about JPA: Can I save the order of the

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.