Questions tagged [object-create]

An alternative to JavaScript's new operator added in ECMAScript 5 and championed by Douglas Crockford.

Object.create() is an alternative to JavaScript's new operator added in ECMAScript 5 and championed by Douglas Crockford.

71 questions
436
votes
11 answers

Understanding the difference between Object.create() and new SomeFunction()

I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with new SomeFunction(), and when you would want to use one over the other. Consider the…
Matt
  • 41,216
  • 30
  • 109
  • 147
391
votes
15 answers

Using "Object.create" instead of "new"

Javascript 1.9.3 / ECMAScript 5 introduces Object.create, which Douglas Crockford amongst others has been advocating for a long time. How do I replace new in the code below with Object.create? var UserA = function(nameParam) { this.id =…
Graham King
  • 5,710
  • 3
  • 25
  • 23
125
votes
5 answers

JavaScript inheritance: Object.create vs new

In JavaScript what is the difference between these two examples: Prerequisite: function SomeBaseClass(){ } SomeBaseClass.prototype = { doThis : function(){ }, doThat : function(){ } } Inheritance example A using…
ChrisRich
  • 8,300
  • 11
  • 48
  • 67
36
votes
5 answers

What is difference between creating object using Object.create() and Object.assign()?

Considering following code: var obj1 = Object.create({}, {myProp: {value: 1}}); var obj2 = Object.assign({}, {myProp: 1}); Is there any difference between obj1 and obj2 since each object has been created in a different way?
madox2
  • 49,493
  • 17
  • 99
  • 99
25
votes
2 answers

Instanceof equivalent for Object.create and prototype chains

Consider such an object with a prototype chain: var A = {}; var B = Object.create(A); var C = Object.create(B); How to check in runtime if C has A in its prototype chain? instanceof doesn't fit as it's designed to work with constructor functions,…
Kos
  • 70,399
  • 25
  • 169
  • 233
19
votes
1 answer

How does object.create work in JavaScript?

Tell me if I'm wrong: A prototype is a normal object. When an object inherits a prototype, it does not just copy the properties of the prototype, the object stores a reference to the prototype. In Firefox, I can do: var food = {fruit:"apple"}; var…
Brian
  • 1,571
  • 1
  • 15
  • 20
17
votes
4 answers

Why is Object.create so much slower than a constructor?

Background In a project I'm maintaining we make extensive use of null prototype objects as a poor man's alternative to (string key only) Maps, which are not natively supported in many older, pre-ES6 browsers. Basically, to create a null prototype…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
15
votes
7 answers

JavaScript inheritance with Object.create()?

How do I inherit with the Object.create()? I tried these, but none are working: var B = function() {}; var A = function() {}; A = Object.create(B); A.prototype.C = function() {}; and var B = function() {}; var A = function() {}; A.prototype.C =…
Tower
  • 98,741
  • 129
  • 357
  • 507
13
votes
3 answers

JavaScript Object.create -- inheriting nested properties

I've come across a peculiarity with Douglas Crockfords Object.create method which I'm hoping someone might be able to explain: If I create an object - say 'person' - using object literal notation then use Object.create to create a new object - say…
Richard
  • 383
  • 3
  • 11
11
votes
4 answers

Is there any reason to use Object.create() or new in JavaScript?

I've been using the new keyword in JavaScript so far. I have been reading about Object.create and I wonder if I should use it instead. What I don't quite get is that I often need to run construction code, so I don't see how Object.create is going to…
Tower
  • 98,741
  • 129
  • 357
  • 507
10
votes
2 answers

Javascript Arrays created with Object.create - not real Arrays?

It looks like Arrays created with Object.create walk like Arrays and quack like Arrays, but are still not real arrays. At least with v8 / node.js. > a = [] [] > b = Object.create(Array.prototype) {} > a.constructor [Function: Array] >…
Philippe Plantier
  • 7,964
  • 3
  • 27
  • 40
10
votes
1 answer

Advantage of using Object.create

Similar to, but different from this question. The code below is from JavaScript: The Definitive Guide. He's basically defining an inherit method that defers to Object.create if it exists, otherwise doing plain old Javascript inheritance using…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
10
votes
5 answers

Understanding Crockford's Object.create shim

I've been reading up on the Crockford shim for preventing the overwriting of prototypes, and understand that it's not the end-all/be-all solution at times. I also understand that ES5 Shim may be a viable alternative to this. I also read this post…
kaidez
  • 679
  • 8
  • 27
9
votes
2 answers

Object.create vs direct prototypical inheritance

I have been playing around with Object.create in the EcmaScript 5 spec, and I am trying to create a multiple inheritance type structure. Say I have a few functions: a, b, and c. With only dealing with prototypes, I can do this: function a ()…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
8
votes
4 answers

Prototypal OO with Object.create and named constructors

I'm coming to Javascript from a background in Python and Smalltalk, and I appreciate the linage of Self and Lisp in the language. With ECMAScript5, I wanted to try my hand at prototypal OO without the new operator. Constraints: optional new…
Shane Holloway
  • 7,550
  • 4
  • 29
  • 37
1
2 3 4 5