/*
* 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.
yeah it should read the properties from
pare kept andoare overwritten.Though when writing this code it is a little safer to do this: