3

If I have an object like this:

var obj = {};

I can't extend it because it hasn't got any prototype.

Is there any way to convert this object to dynamic so that it's possible to extend it and use new keyword. Something like:

obj.prototype.property = 'value';

var newobj = new obj;
RuntimeException
  • 1,135
  • 2
  • 11
  • 25

2 Answers2

1

That has nothing to do with static or dynamic.

You can only use the new operator on functions, not objects.

You cannot turn an object into a function; you need to create it as a function in the first place.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • By saying 'dynamic', I mean ability to extend it. So is it impossible to use prototype with objects mentioned in question? – RuntimeException Sep 10 '13 at 16:10
  • 1
    @RuntimeException: You're misunderstanding `prototype`. Prototypes have nothing to do with "extending" objects; they are only used for constructor function. You can create any property you want on any object. – SLaks Sep 10 '13 at 16:12
1

the only way is the following:

var obj = function () {};

because you can only use the new keyword with constructor function. That's it!

tikider
  • 540
  • 3
  • 12