1

Lets say I have 100 objects

var a={name:'a'};
var b={name:'b'};
...
...
var xyz={name:'xyz'};

Now I want to add a method to all of these objects which will do something similar for all objects, let's say display the property 'name'. How can I add a method to the Object.prototype of all of these objects.

abdul
  • 340
  • 6
  • 18
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto see the various warnings at the top. – castis Jun 19 '18 at 20:04
  • Do you want the `name` property be equal to the variable name? – Praveen Kumar Purushothaman Jun 19 '18 at 20:04
  • You may want to establish an es6 class and use that to create your multiple instances. Since you plan on making so many, you could attach that method to the prototype of the class rather than placing the method directly inside. – Devin Fields Jun 19 '18 at 20:08
  • you can append to `Object.prototype`, but that's frowned upon. you can use Object.assign to dupe, or just tack on a method to each instance in a loop. – dandavis Jun 19 '18 at 20:08
  • @dandavis I found that, instead of using Object literals, using a constructor function would be much easier. Thanks for the support. – abdul Jun 19 '18 at 20:37

3 Answers3

1

You can do the same by making one class for all objects and add prototype method (answer for why not class method) for the same.

Here's example for the same as per question -

class MyObject {
  constructor(obj) {
    this.obj = obj;
  }
}

var a = new MyObject({
  name: 'a'
});
var b = new MyObject({
  name: 'b'
});
var xyz = new MyObject({
  name: 'xyz'
});

MyObject.prototype.getName = function() {
  return this.obj.name;
}

console.log(a.getName());
console.log(b.getName());
console.log(xyz.getName());
Vivek
  • 1,465
  • 14
  • 29
  • As I said in my comment on the question post, since he is creating so many of these, he may want to instead declare "getName" on the prototype of the myObject class rather than directly on the class, as in your example. Both will work, obviously, but placing the method directly on the class will reinstantiate the method for every instance. – Devin Fields Jun 19 '18 at 20:09
  • @DevinFields I've edited my answer by looking into trad off of prototype vs class function and also added reference for the same – Vivek Jun 19 '18 at 20:14
0

You can create a "template" for a group of object like this:

function myObj() {
  this.name = arguments[0];
};

In the code above the "template" (myObj) is just a JavaScript function which receive N arguments (other alternatives can be implemented).

Now, in order to create objects which use this "template" you just need to instantiate them like this:

var a = new myObj('a');

See example below.

function myObj() {
  this.name = arguments[0];
};

var a = new myObj('a');
var b = new myObj('b');
var xyz = new myObj('xyz');

console.log(a.name);
console.log(b.name);
console.log(xyz.name);
lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
0

Using Object.create(), you could create your own "prototype" and use that to create all of your objects then.

let myProto = {
  func() {
    return this.name;
  }
}

let a = Object.create(myProto);
a.name = 'a';

// or using Object.assign()
let b = Object.assign({name: 'b'}, myProto);

// Object.create assigns to __proto__
console.log(a.__proto__);

// Object.assign doesn't, it assigns as a member function
console.log(b.__proto__);
console.log(b.func);

console.log(a.func())
console.log(b.func())
baao
  • 71,625
  • 17
  • 143
  • 203