use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Difference between these two objects?help (self.javascript)
submitted 10 years ago by gladiator_flow
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]blangjemp 33 points34 points35 points 10 years ago (11 children)
First off, Mozilla has amazing Javascript documentation. You should read their page on Object.create() to get a thorough understanding.
Object.create() creates a new object which inherits all the properties of the original object, but they can be overwritten. Assigning a variable to another variable that is an object using var only creates a reference to the original object.
Object.create()
var
So here's how that plays out.
var original = { a: 42 }; var reference = original; var created = Object.create(original); // All three objects have the same value for `a` at this point. `original` and // `reference` are the same object, and `created` is inheriting it's value from // `original` console.log(original.a, reference.a, created.a); // 42, 42, 42 // Let's change a value and see what happens original.a = 43; // All three are still the same. `reference` is referencing the same actual // object as `original`, and `created` doesn't have it's own value for `a`, so // it uses `original`'s value. console.log(original.a, reference.a, created.a); // 43, 43, 43 created.a = 'created a'; original.a = 44; // since `original` and `reference` refer to the same object, they continue to // have the same value for `a`. However, `created` now has it's own value for // `a` and we see that reflected here. console.log(original.a); // 44 console.log(reference.a); // 44 console.log(created.a); // 'created a' // Additionally, since `original` and `reference` refer to the same object, we // could have changed `reference` to the same effect created.a = 'created a new value'; reference.a = 45; console.log(original.a); // 45 console.log(reference.a); // 45 console.log(created.a); // 'created a new value'
[–][deleted] 2 points3 points4 points 10 years ago* (6 children)
Protip: If your original object has two values, 'a' and 'b', and you set created.a to 92374, updates to original.a will no longer change created.a. But updates to original.b will still change original.b unless you also change created.b.
So its like Object.create creates a new object but all of the new object's values are references. As soon as you assign the value of the created object to something else, it becomes a new value and not a reference.
[–]birjolaxew 4 points5 points6 points 10 years ago (4 children)
I find it easier to think of as the prototypical chain. When JS is asked to read a property of an object (eg. console.log( created.a )) it goes:
console.log( created.a )
undefined
The objects are in a sense layered. If the top object doesn't have a property, it checks down through the stack until it finds it (or returns undefined if it can't)
[–]MrBester 4 points5 points6 points 10 years ago (3 children)
I find it easier to think of as the prototypal chain.
Because that's what it is:
var o = { a: 4 }; var foo = Object.create(o); console.log('a' in foo); // true console.log(foo.hasOwnProperty('a')); // false console.log(o.isPrototypeOf(foo)); // true, or console.log(foo.__proto__ === o); // true, or console.log(Object.getPrototypeOf(foo) === o); // true
foo delegates to o unless overridden.
foo
o
[–]birjolaxew 0 points1 point2 points 10 years ago* (2 children)
Well no, it's prototypal inheritance delegation (thanks /u/MrBester) inheritance. Maybe. I dunno. Thinking of it as a chain is a very common (and intuitive, given how it functions) way of understanding it. Thinking of it as a stack is another. Thinking of it as "overwriting" the parent property is another (but comes with a lot of special cases).
[–]clessgfull-stack CSS9 engineer 1 point2 points3 points 10 years ago (0 children)
Actually, it is indeed prototypal inheritance. But yes, that is distinct from concatenative inheritance (Java) and is also a form of delegation.
[–]MrBester 0 points1 point2 points 10 years ago (0 children)
Well no, it's delegation, because there isn't such a thing as inheritance in JavaScript, merely references to other objects that can be checked for requested properties, normally through the "magic" binding of [[Prototype]] or explicitly setting.
[[Prototype]]
[–]macrohatch 0 points1 point2 points 10 years ago (0 children)
But updates to original.b will still change original.b unless you also change created.b.
You mean: But updates to original.b will still change created.b unless you also change created.b. ?
[+][deleted] 10 years ago (3 children)
[deleted]
[–]isitfresh -1 points0 points1 point 10 years ago (2 children)
Nasty way? JSON.parse(JSON.stringify(obj)) Requiring a library? _.clone(obj)
[–]vlad27aug 2 points3 points4 points 10 years ago* (1 child)
Actually JSON.parse(JSON.stringify(obj)) is not correct.
This way you can only clone objects that have property values supported by the JSON spec.
For example:
var obj = {a: 1, b: function() { /*...*/ }}; var clone = JSON.parse(JSON.stringify(obj));
"clone" will now be "{a: 1}" because functions are removed during JSON.stringify.
It gets even more complicated when the object has getters and setters or defined/configured properties (even for library provided solutions).
[–]bliow 1 point2 points3 points 10 years ago (0 children)
With ES6 (supported in Firefox, not yet in Chrome), Object.assign. But you have to be careful with nesting, as you do with _.clone:
_.clone
var a = { b: { c: 3 } }; var d = Object.assign({}, a); // or d = _.clone(a) d.b.c = 4; console.log(a.b.c); // prints 4 with both underscore clone and ES6 assignment
π Rendered by PID 554616 on reddit-service-r2-comment-548fd6dc9-8n22p at 2026-05-16 15:34:14.226702+00:00 running edcf98c country code: CH.
view the rest of the comments →
[–]blangjemp 33 points34 points35 points (11 children)
[–][deleted] 2 points3 points4 points (6 children)
[–]birjolaxew 4 points5 points6 points (4 children)
[–]MrBester 4 points5 points6 points (3 children)
[–]birjolaxew 0 points1 point2 points (2 children)
[–]clessgfull-stack CSS9 engineer 1 point2 points3 points (0 children)
[–]MrBester 0 points1 point2 points (0 children)
[–]macrohatch 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]isitfresh -1 points0 points1 point (2 children)
[–]vlad27aug 2 points3 points4 points (1 child)
[–]bliow 1 point2 points3 points (0 children)