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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:49:05+00:00 2026-05-24T13:49:05+00:00

/* * Copy the enumerable properties of p to o, and return o. *

  • 0
/*
* Copy the enumerable properties of p to o, and return o.
* If o and p have a property by the same name, o's property is overwritten.
* This function does not handle getters and setters or copy attributes.
*/
 function extend(o, p) {
       for(prop in p) { // For all props in p.
           o[prop] = p[prop]; // Add the property to o.
      }
      return o;
}



/*
* Return a new object that holds the properties of both o and p.
* If o and p have properties by the same name, the values from o are used.
*/
     function union(o,p) { return extend(extend({},o), p); }

I think for union, he meant “values from p are used”.

I did the test on Chrome. Am I wrong? Sorry. I tend to be very caution when I am learning, especially this is the #1 book for Javascript, and 6ed is recent.

var o = {x:1}

var p = {x: 2}

function extend(o,p){

for(prop in p)
    o[prop] = p[prop];
return o;

}

function union(o,p){

return extend(extend({},o),p);

var g = union(o,p)

g.x

2

Thank you.

  • 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-24T13:49:06+00:00Added an answer on May 24, 2026 at 1:49 pm

    yeah it should read the properties from p are kept and o are overwritten.

    Though when writing this code it is a little safer to do this:

    for(var prop in obj) {
        if(obj.hasOwnProperty(prop)) {
            // now you know it is actually a property on obj and not inherited from elsewhere
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two C# classes that have many of the same properties (by name
I have a function that accepts an Enumerable. I need to ensure that the
I copy a script of Kevin S (member of this site) like this <table
I copy-pasted from MSDN this code: using System.Security.Cryptography; byte[] buffer = enc.GetBytes(text); SHA1CryptoServiceProvider cryptoTransformSHA1
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy is aimed at fast
I copy and paste code from this URL for creating and reading/writing a proc
i have such code (copy paste from wiki). Its multiplication of those big numbers
Constructor Delay.vkMessages.vkMessages(string, System.DateTime, string, bool, string) cannot call itself.I have another class, copy of
I was trying to copy an enumerable for the purposes of a comparison as
Javascript - The Definitive Guide (6ed) shows the following code: Originally we have this

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.