0

When I create an object using a constructor like:

function Person(name, age) {
  this.name = name;
  this.age = age;
 }

I can add properties that are functions to the constructor function which act like (static) class methods. Object.create doesn't seem to support that. Is that right? The constructor property of all object created by Object.create seem to be the same function.

Thanks in advance.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Fred Finkle
  • 1,947
  • 3
  • 18
  • 21
  • Note that javascript is a [prototype-based language](http://en.wikipedia.org/wiki/Prototype-based_programming); it can indeed _simulate_ [classical inheritance](http://en.wikipedia.org/wiki/Class_(computer_programming)#Definitions_of_subclass), but that doesn't change its nature. – Félix Saparelli Sep 20 '12 at 01:27
  • I don't see how `Object.create` would have anything to do with a "class". Can you show us how you are using it? – Bergi Jan 31 '13 at 11:41
  • possible duplicate of [difference between Object.create() and new SomeFunction()](http://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction-in-j) or [inheritance: Object.create vs new](http://stackoverflow.com/questions/13040684/javascript-inheritance-object-create-vs-new) – Bergi Jan 31 '13 at 11:44

1 Answers1

0

Are you sure you mean static?

What I mean is, in a more class-dependent language, you might have:

class Person {

    static people = 0;

    public name;
    public age;

    public Person (name, age) { this.name = name; this.age = age; Person::people += 1; }
    public sayName () { echo this.name; }

    static numPeople () { echo Person::people; }
}

Then you might say:

bob = new Person("Bob", 32);
bob.sayName(); // "Bob"
Person::numPeople(); // 1

If you wanted Person::numPeople(); functionality, there's nothing stopping you from adding Object.create.numPeople = function () {};

The question you might want to ask yourself is "Why?"

Are you trying to call static methods like:

bob.constructor.numPeople();

If so, I'm sure that there's a better way around that. For example, extending the constructor's prototype would provide access to static properties/methods by default.

var personObj = {
    sayName : function () { console.log(this.name); },
    sayAge : function () { console.log(this.age); }
};

var bob = Object.create(personObj, { name : "Bob", age : 32 });

These are accessed in a way which is similar to traditional inheritance, but static in the sense that each object references the same functions and values, so if that prototype object changes, each constructed instance changes, as well.

Personally, I prefer the freedom of doing most of my object creation inline and on-demand. If I need to create multiples of the same format, then I'll typically create a factory, rather than creating a constructor or using Object.create.

With a factory, even just a simple one, using one or two levels of closure, you can simulate private properties/methods, and with the second closure, you can simulate "class-wide" private-statics.

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • 1
    The code: "Object.create.numPeople = function () {};" adds a property on the Object constructor. Not what I want to do (or should anyone). I do want something like "bob.constructor.numPeople();" but I would write it as "Person.numPeople()" If I were to use the factory approach I would want it to be called as "Person.factory(...)". The last example "Object.create(personObj, {name:"Bob", age:32}" doesn't work. The second argument should be property descriptors (ECMA 5) – Fred Finkle Sep 20 '12 at 15:30
  • You're right -- that's me having a brain fart at the end of the day. That said, for your needs, stick with either a constructor which you extend yourself, or a factory pattern, which, again, you can extend at will. JS has limited use for actual constructors, when you can frequently use one closure or two, to provide "inheritance" or "static" properties, as readily as having an IIFE which returns a function. – Norguard Sep 20 '12 at 15:44