1

When I run the following code

var ExtendedObject = function() {
  this.standard = 5;
};

Object = ExtendedObject.bind(Object);
var p = new Object();
console.dir(p.standard);

, the output is

5

as expected.

If I instead instantiate the variable p as an object literal like this:

var ExtendedObject = function() {
  this.standard = 5;
};

Object = ExtendedObject.bind(Object);
var p = {};
console.dir(p.standard);

The result is

undefined

I am trying to find a way to modify the constructor of Object such that I can add some standard content to all new objects being created.

4thex
  • 1,094
  • 1
  • 9
  • 21
  • when `var p = {};` is empty then result would be undefined Obviously, yes try `var p = Object` it would work – Abdul Rafay Dec 21 '17 at 06:02
  • What is `.bind(Object)` supposed to do? – Bergi Dec 21 '17 at 06:22
  • The `this` in a constructor function is different from to the `this` in object methods by "definition". The `this` in the constructor always refers to the new object to be instantiated. Accordingly you can not `bind` it to any ambigious context. – Redu Apr 01 '18 at 18:35

4 Answers4

3

No, it is absolutely impossible to redefine how an object literal evaluates; it will always become a native object with the builtin Object.prototype, not a subclass or anything and it will also not invoke a custom constructor.

This is in fact a security feature, as it prevents JSON hijacking.

… such that I can add some standard content to all new objects being created

That's a horrible idea and will break every library you'd ever use in this environment (including all functions you'd write yourself). However, if you insist on having a common (not individual!) property on all objects, you might consider defining it on Object.prototype. Not that I would recommend it, but at least do it correctly.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You can create a class and extend it from Object.

class MyObject extends Object {
  constructor() {
     super();
     this.standard = 5;
  }
}

const obj = new MyObject();
console.log(obj);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • This is ES6 implementation and is supported by most of the modern browsers. Feel free to use it, For more Info refer http://es6-features.org/#ClassInheritance – Ashish Yadav Dec 21 '17 at 06:10
  • 1
    I think OP's goal is to have all objects inheriting from Object have its set properties. i.e even object literals would have the `standard` property. – Kaiido Dec 21 '17 at 06:21
  • I did not want that. I basically want to add functionality to an application without modifying what is already written. This is a principle of BDD - Behavior Driven Design. – 4thex Dec 21 '17 at 12:57
1

Define the property on the prototype of Object like this:

Object.defineProperty(Object.prototype, 'standard', {
  value: 5,
  writable: true,
  enumerable: true,
  configurable: true
});
var p = {};

console.dir(p.standard);
4thex
  • 1,094
  • 1
  • 9
  • 21
-1

This may be a helpful link that discuss about JavaScript objects. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Suresh
  • 14
  • 4
  • Please point out which part of that article that answers my question. I fail to see it. – 4thex Dec 21 '17 at 13:00