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
7
votes
1 answer

Object.create alters console output of proto object in Chrome?

I was playing around today when I noticed that some of my objects in Chrome's console were being displayed as Object instead of the constructor function name. This was odd, so I boiled it down to the following code: function Baz() { this.baz =…
pbo
  • 537
  • 4
  • 13
7
votes
2 answers

Passing arguments when using Object.create as opposed to new

This question isn't a duplicate of Using "Object.create" instead of "new". The thread in question doesn't focus on passing arguments correctly when using Object.create I am curious as to how I would go about initializing objects using…
Sethen
  • 11,140
  • 6
  • 34
  • 65
6
votes
2 answers

How to prevent changes to a prototype?

In this code, the prototype can still change. How I can prevent changes to the prototype? var a = {a:1} var b={b:1} var c = Object.create(a) Object.getPrototypeOf(c) //a c.__proto__ = b; Object.getPrototypeOf(c) //b var d =…
Oksana
  • 300
  • 2
  • 9
6
votes
1 answer

Prototypal inheritance: Can you chain Object.create?

I'm new to prototypal inheritance so I'm trying to understand the 'right' way. I thought I could do this: if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return…
6
votes
2 answers

Object.create method in javascript

Being a beginner in javascript, i tried to understand Object.create() method from here https://developer-new.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create In the example code, line 18. A accessor property is created with…
John Eipe
  • 10,922
  • 24
  • 72
  • 114
5
votes
5 answers

How to emulate a constructor with ES5 Object.create and object literal syntax?

Presume I have an object like this: var Foo = { x: 5, sprite: new Image() } Problem: I want to initialize that sprite with the right src. However, when I use the following creation technique: var f = Object.create(Foo); I don't have a…
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
5
votes
5 answers

Benefits of Avoiding 'new' Keword When Creating Object in Java

I have found some blog where there is a suggestion of avoiding new keyword while creating object of a class. Some examples of creating object without the new keyword are- SampleObject obj =…
Razib
  • 10,965
  • 11
  • 53
  • 80
5
votes
3 answers

Can javascript constructor function and object.create be combined?

Update If this is not possible, please feel free to provide an answer explaining why. I'd be happy to mark as it accepted. I'd like to slightly simplify the following code (two steps for an object "declaration", I'd like to have one): var Masher =…
Andrew Philips
  • 1,950
  • 18
  • 23
5
votes
3 answers

How to get to my ancestor’s overridden method using Crockfords's Object.create() (Javascript)

if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } var o1 = {}; o1.init = function(){ alert('o1'); }; var o2 = Object.create(o1); o2.init…
Jan
  • 6,532
  • 9
  • 37
  • 48
4
votes
1 answer

From which version, IE can support Object.create(null)?

You can create an object in JavaScript in many ways: // creates an object which makes the Object, prototype of data. var data1 = new Object(); // Object literal notation; Object still is the prototype of data2. var data2 = {}; // anotherObject…
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
4
votes
3 answers

Javascript Object.create not working in Firefox

I always get the following exception in Firefox (3.6.14): TypeError: Object.create is not a function It is quite confusing because I am pretty sure it is a function and the code works as intended on Chrome. The lines of code responsible for this…
dominos
  • 412
  • 1
  • 11
  • 21
4
votes
2 answers

Add Function to JavaScript Object using Object.Create

I'm trying to use Object.Create in JavaScript. I currently have the following code: var vehicle = { getModel: function () { console.log( "The model of this vehicle is.." + this.model ); } }; var car = Object.create(vehicle, { …
davy
  • 4,474
  • 10
  • 48
  • 71
4
votes
1 answer

Object.create Prototype Chains

Initial Question Yesterday i read about ECMAScript 5 Object.create() And I wanted to start building prototype Chains in my Code with this method instead of setting the prototype and its constructor, I like that you can directly set writable…
4
votes
3 answers

Why does Object.create make my private variables static?

Code is available here to play with - http://jsfiddle.net/dsjbirch/zgweW/14/ This is basically a straight copy and paste of crockfords explanation of private variables. I have added Object.create() and a bit of tracing. Why does the second object…
bluekeys
  • 2,217
  • 1
  • 22
  • 30
3
votes
1 answer

Accessing get/set methods for JavaScript property created using Object.create

What I'd like is the ability to wrap a JavaScript property to modify behavior on get/set. For properties that are values, I can do the following: var obj = { myProperty : 0 }; function notifyOfChange(obj, propertyName) { var propertyValue =…
guy
  • 587
  • 4
  • 13