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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:32:40+00:00 2026-06-13T20:32:40+00:00

Let’s say I want to use ONLY object literals (not constructors). I have an

  • 0

Let’s say I want to use ONLY object literals (not constructors). I have an object like this:

var o = {
  name : "Jack"
}

If I want to create another object which its prototype is o I use this syntax:

var u = Object.create( o );
console.log( u.name );//prints Jack
u.name = "Jill";
console.log( u.name );//prints Jill

Works fine! No problem. But now at runtime I want to change the prototype of u to something else. If u was created with a constructor like this:

function U () {}
U.prototype.name = "Jack";
var u = new U;
console.log( u.name );//prints Jack

OR

function U () {
  this.name = "Jack";
}
var u = new U;
console.log( u.name );//prints Jack

But when using constructors, I can totally change the prototype:

function U () {}
//totally changed the prototype object to another object
U.prototype = {
  dad : "Adam"
}
var u = new U;
console.log( u.dad );//prints Adam

Then whatever I added to the prototype of U would automatically be added to every object that is created after those changes. But how do I get the same effect with Object literals?

Please provide the simplest solution which has a clean and short syntax. I just wanna know if it’s possible to do this manually. I don’t want to use the non-standard __proto__ keyword either.

I’ve searched Stackoverflow and this is not really a duplicate of the following questions:

  • Adding Prototype to JavaScript Object Literal
  • Adding a prototype to an object literal

Because I want to change the prototype after creation in a standard way (if possible). Something like Object.setPrototype() would be perfect, but that function doesn’t exist! Is there any other way to simply set the prototype of an object that is created using object literal initialization?

  • 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-13T20:32:41+00:00Added an answer on June 13, 2026 at 8:32 pm

    With the current spec, you can’t change an object’s prototype once it’s instantiated (as in, swap out one and swap in another). (But see below, things may be changing.) You can only modify the object’s prototype. But that may be all you want, looking at your question.

    To be clear about the distinction:

    var p1 = {
        foo1: function() {
            console.log("foo1");
        }
    };
    var p2 = {
        foo2: function() {
            console.log("foo1");
        }
    };
    
    var o = Object.create(p1);
    o.foo1(); // logs "foo1"
    o.foo2(); // ReferenceError, there is no foo2
    // You cannot now *change* o's prototype to p2.
    // You can modify p1:
    p1.bar1 = function() { console.log("bar1"); };
    // ...and those modifications show up on any objects using p1
    // as their prototype:
    o.bar1(); // logs "bar1"
    // ...but you can't swap p1 out entirely and replace it with p2.
    

    Getting back to your question:

    If u was created with a constructor like this…Then whatever I added to the prototype of U would automatically be added to every object that is created after those changes. But how do I get the same effect with Object literals?

    By modifying the object you passed into Object.create as the prototype, as above. Note how adding bar1 to p1 made it available on o, even though o was created before it was added. Just as with constructor functions, the prototype relationship endures, o doesn’t get a snapshot of p1 as of when it was created, it gets an enduring link to it.


    ES.next is looking likely to have the “set prototype operator” (<|), which will make it possible to do that. But there’s no standard mechanism for it currently. Some engines implement a pseudo-property called __proto__ which provides this functionality now, but it’s not standardized.

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

Sidebar

Related Questions

Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I use a custom controller to have a url path/frontend name of
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let's say I have the following data frame: > myvec name order_no 1 Amy
Let I have an array like a <- seq(1, 100, 1) and I want
Let's say I have this element for displaying the website logo: <div id="web-title"> <a

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.