0

With a associative array such as:

var m = {};
m['a'] = {id:1, foo:'bar'};
m['b'] = {id:2, foo:'bar'};

Is it possible to create a prototype such as:

Array.prototype.hello = function() { console.log('hello'); }
m.hello();

This fails because m is an object, so I tired:

Object.prototype.hello = function() { console.log('hello'); }

and this is problematic too.

Is is possible to create a prototype which can operate on this data structure?


Update: I think I need some sleep :)

When I create and use the Object.prototype.hello = function() { console.log('hello'); } by itself it works fine.

When I add the prototype and include a 3rd party JS Framework, it makes the framework stop working.

0

4 Answers 4

2

Why not create your own object constructor so that you can extend its prototype without issues?

function O(o) {
    for (var p in o) {
        this[p] = o[p];
    }
}

O.prototype.hello = function() { console.log('hello') }

Then use the constructor with your object literals.

var m = new O({})
m['a'] = {id:1, foo:'bar'}
m['b'] = {id:2, foo:'bar'}

There are tricks that'll let you drop the new if you wish.

Sign up to request clarification or add additional context in comments.

Comments

1

You can assign custom properties to any object, and that means you can do so on an object with a different immediate underlying prototype than Object.prototype. So you could do this, for instance:

function MyMap() {
}
MyMap.prototype.hello = function() {
    console.log('hello');
};
var m = new MyMap();
m['a'] = {id:1, foo:'bar'};
m['b'] = {id:2, foo:'bar'};
m.hello();

Note, though, that if you stored a hello entry:

m['hello'] = {id:3, foo:'bar'};

...it would hide the hello that your object gets from the prototype.

Also note that your m will have the properties not only from MyMap.prototype, but also from Object.prototype (like {} does), like toString and valueOf and hasOwnProperty. If you want to not have Objectproperties, you can do that, too:

function MyMap() {
}
MyMap.prototype = Object.create(null);
MyMap.prototype.hello = function() {
    console.log('hello');
};

Also note that constructor functions (MyMap, above) are only one way to create objects with an underlying prototype. You can just use Object.create directly:

var mapPrototype = {
    hello: function() {
        console.log('hello');
    }
};
var m = Object.create(mapPrototype);

2 Comments

I am going to try your last suggestion. Thank you for your help.
@BrianMcGinity: No worries. Note that it has the same conflict issue that all the others do. :-) The only way around the conflicts problem would be to store the entries on a different object, like ES6's map conceptually does.
1

You could use Object.create to create an array-like prototype for your structure.

var proto = Object.create(Array.prototype);
proto.hello = function() { console.log('hello'); }

Then use it like

var stack = Object.create(proto);
stack.hello();
stack.push('example');

Comments

0

Assigning to Object.prototype should work just fine. When running in node:

> Object.prototype.foo = function() { console.log("foo!"); }
[Function]
> var m = {};
undefined
> m.foo();
foo!
undefined

Whether it is a good idea is a whole other discussion...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.