JS assignment order of properties in objects

Posted by devdev on Stack Overflow See other posts from Stack Overflow or by devdev
Published on 2011-06-29T14:22:22Z Indexed on 2011/06/29 16:22 UTC
Read the original article Hit count: 212

Just had a quick question about why a certain order of assignment works and another doesn't.

I wanted to create a simple "inherit" / "copy" function (just for testing it) that copies properties from one object to another:

var cat = { tail:"yes", hairy:"yes, hairy" };
var dog = { sick:"extremely ill"};

function inherit(obj1, obj2) {
    for (var p in obj1) 
    {
    obj2[p] = obj1[p]; // this works, but "obj1[p] = obj2[p];" doesn't. Why??
    }
}

inherit(cat, dog);

console.log(dog.tail);

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about properties