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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:00:04+00:00 2026-05-17T17:00:04+00:00

Using Crockford’s method ( http://javascript.crockford.com/prototypal.html ) doesn’t work when we have reference values in

  • 0

Using Crockford’s method ( http://javascript.crockford.com/prototypal.html ) doesn’t work when we have reference values in properties (an array in the supertype) as this is common among all the objects.

So, what’s really the recommended way to do object inheritance in Javascript that doesn’t have problem and that visually encapsulates better all the object’s methods and properties?

Example:

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
var p1 = {name: "nick", friends : ["a1", "a2"]};
var p2 = Object.create(p1);
p2.name ="john";
p2.friends.push("andre");
alert(p1.friends);
alert(p2.friends);
alert(p1.name);
alert(p2.name);

The friends array returns the same values for both p1 and p2, while I expect that it returns different values.

  • 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-17T17:00:04+00:00Added an answer on May 17, 2026 at 5:00 pm

    Well, the members higher in the prototype chain are “shared”, that’s the goal of prototypal inheritance…

    However, you can assign an own property to the newly created object (just like you do with the name property, for example:

    var foo = {foo: "xxx", array : ["a1", "a2"]};
    
    var bar = Object.create(foo);
    bar.foo = "yyy";
    bar.array = ["b1", "b2"];
    bar.array.push("bar");
    
    alert(foo.array);   // ["a1", "a2"]
    alert(bar.array);    // ["b1, "b2", "bar"]
    alert(foo.foo);     // "xxx"
    alert(bar.foo);     // "yyy"
    

    The standard Object.create method, available on the latest browser versions, provides a way to create own properties, using a second argument, for example you could:

    // ...
    var bar = Object.create(foo, {
      foo: { value: 'yyy'},
      array: {value: ["b1", "b2"] }
    });
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read about Crockford's push for using JavaScript in a more obviously prototypal
I'm currently using a JavaScript coding style based on Douglas Crockford's Code Conventions for
I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes
In OO Javascript constructor pattern: neo-classical vs prototypal , I learned that constructors using
Using Java,I have to fetch multiple sets of values from an XML file to
I have read Crockford's JavaScript: The Good Parts and have used his validator JSLint
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
I have the following code, using Crockford's json2 to parse the object into json
Just finished reading Crockford's JavaScript: The Good Parts and I have a question concerning
I'm writing a rudimentary lexer using regular expressions in JavaScript and I have two

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.