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
3
votes
3 answers

Object.create in NodeJS

Object.create works differently in Nodejs compared to FireFox. Assume an object like so: objDef = { prop1: "Property 1" } obj = { prop2: "Property 2" } var testObj = Object.create(obj, objDef); The above javascript works perfectly in Mozilla.…
Barry Steyn
  • 1,563
  • 3
  • 19
  • 25
3
votes
3 answers

Object.create and reference properties

How do you deal with the problem of sharing a prototype's reference property among its children? Is there any well-known pattern (other then redefining the property inside the constructor) to solve this problem? Is this a problem at all, or there is…
Raphael
  • 1,847
  • 1
  • 17
  • 29
3
votes
2 answers

Object.create and Prototypes

When I use object.create to create a new object like so, o = {x:1,y:2}; p = Object.create(o); I'm under the impression that o becomes the prototype of p and inherits all its methods. Then why, when I try print(p.prototype); the output is…
Fawkes5
  • 1,001
  • 3
  • 9
  • 12
3
votes
1 answer

Setting prototype on object created through Object.create

I'm having some trouble figuring out how the prototype chain is set up given the following code. var Model = { prototype: { init: function(){}, log: function(){ console.log('instance method log was called') } }, log:…
jhummel
  • 1,724
  • 14
  • 20
2
votes
1 answer

Javascript object.create and isPrototypeOf

var Person = function(name, age){ return Object.create(Object.prototype, { name: { value: name | "", writable: true, enumerable: true, configurable: true }, age: { …
leepowell
  • 3,838
  • 8
  • 27
  • 35
2
votes
1 answer

Most optimized way to create multiple objects with several operations

So I've been looking into factory functions and classes, looking for the most optimized way of creating multiple objects with several functions/operations each. Let's say I am doing a TODO list, every task I create has 7 parameters and even more…
spacing
  • 730
  • 2
  • 10
  • 41
2
votes
1 answer

Js - solving challenge Range Sum Query - Immutable with creating object and its properties

I was going through the problems on leetcode site, and saw a problem that I don't know how to approach. This is how they explain it: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example:…
Leff
  • 1,968
  • 24
  • 97
  • 201
2
votes
2 answers

Object.create and built-in objects

I am trying to add functionality (new methods) to a built-in object (in my case, of type CanvasRenderingContext2D). The first approach was to add the methods to the prototype, it works, but I would prefer not to modify built-in objects. I was…
2
votes
2 answers

Constructor of the object created using Object.create(someprototype) in javascript

Objects created using Object.create(someObj.prototype) has it's constructor as someObj, then how come when I try to access the properties of someObj, then it gives as undefined? function foo(){ this.name1 = "Name"; this.otherName1 =…
nitte93
  • 1,820
  • 3
  • 26
  • 42
2
votes
2 answers

Using Object.create in [google-apps-script] script file in Chrome on a Chromebook

I have been trying to use Object.create in a gas script file. Object.create is defined but doesn't seem to return a useful object. function createOject() { var o = Object.create({}, { p: { value: 42 } }) Logger.log(o.p); //logs 42.0 as…
user1472330
  • 123
  • 1
  • 10
2
votes
2 answers

Javascript private variables + Object.create (reference to closure variables)

I was wondering how I could make private variables in javascript through clojure. But still have them cloned when using Object.create. var point = {}; (function(){ var x, y; x = 0; y = 0; Object.defineProperties(point, { "x": { set:…
automaticoo
  • 868
  • 7
  • 24
1
vote
1 answer

Object.create and inheritance

What is the difference between the resulting objects in the following examples: var EventEmitter = require('events').EventEmitter; var oProto = Object.create(EventEmitter.prototype); var oProto2 = Object.create(oProto); var oConstr =…
Jacob Rask
  • 22,804
  • 8
  • 38
  • 36
1
vote
1 answer

Using Object.create in a forEach loop

I am writing a card game using javascript/html5 I get the gamestate as an ajax request. this is JSON data that lists the players and what cards they have in their hand I am trying to loop over each player and set the hand data as…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
1
vote
1 answer

Defining a class with or without the .prototype

In the absence of using the class keyword, what is the difference between the following two ways to construct an inherited object? In the first one I'm attaching things on the .prototype and in the second one I'm doing it directly on the function.…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
3 answers

How to use Object.create for inheritance within modules in node.js

I have a module Vehicle that contains general vehicle info. I have another module Car, which adds more functionality to Vehicle object. // Pseudo code only. The final functions do not have to resemble this var vehicle =…
baalexander
  • 2,579
  • 1
  • 25
  • 32