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

Phil Haack's blog entry describes this process using ASP.NET MVC 2's futures and Crockford's
So I finished watching Douglas Crockford's excellent series on Javascript, and in the final
Javascript (ECMAscript) supports the Array.prototype.forEach method since version 1.6 (ECMAscript edition 3, 2005). So
I have a medium size legacy php application with almost no javascript integration. I've
According to Crockford's json.org , a JSON object is made up of members ,
Using just python, is it possible to possible to use a USB flash drive
using select statement we can call functions inside the stored procedure, is there another
Many people say that you should avoid new Object, new Array()and instead use {}.
I started out with Rails development, but soon I realized Rails without JS is

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.