1

So I'm trying to find a really clean JavaScript function prototyping pattern.

I can do this:

var Person = function () {
  // person logic
}.prototype.foo = function () {
  // people's foo logic
}

but I want to name my class constructor without being redundant. The following code yield unexpected . before the word prototype.

function Person() {
  // person logic
}.prototype.foo = function () {
  // people's foo logic
}

How can I define my prototype on the same line without redundantly setting Person to a variable?

alt
  • 13,357
  • 19
  • 80
  • 120
  • 1
    You might prefer a pattern that uses `Object.create()`. http://blog.javascriptroom.com/2013/01/21/the-initializer-pattern/ – m59 Dec 07 '13 at 03:14
  • Does your first code really work? – Oriol Dec 07 '13 at 03:18
  • 1
    Setting `Person` to a variable wouldn't be "redundant". It would be done exactly once, and would be needed to access it anyway. – Blue Skies Dec 07 '13 at 03:19
  • Even if you use function declaration instead of function expression, you will also have the variable `Person` (otherwise, how to you plan to create instances?) – Oriol Dec 07 '13 at 03:25

2 Answers2

2

"How can I define my prototype on the same line without redundantly setting Person to a variable?"

Like this (there's absolutely no redundancy here):

var Person; (Person = function() {

}).prototype.foo = function() {

};

You need a separation of the declaration of the variable and a grouping of the first assignment in order to deal with the lower precedence of the = operator, as well as its right-to-left associativity.

But IMO, there's no benefit to this. Keep your code simple and clear.


Both your question and comment imply that you think there's some redundancy here. There is none.

Your requirement seems arbitrary, but if you really wanted this, then pass the function to a helper function that puts the pieces together.

function why(ctor, members) {
    for (var m in members)
        ctor.prototype[m] = members[m];
    return ctor;
}

And use it like this:

var Person = why(function() {

}, {
    foo: function() {

    }
});

Again, no real benefit, and only added overhead, but it fulfills your requirement.

Blue Skies
  • 2,935
  • 16
  • 19
  • I'd like to only specify the function's name once. – alt Dec 07 '13 at 03:18
  • @JacksonGariety: It is specified only once. Do you mean you only want to "type" it once? Well, sorry. What you want can't be done without passing the function and prototype members to a helper. – Blue Skies Dec 07 '13 at 03:19
0

Is it possible? The most compact mode I know is:

var Person = function() {
    this.func = function() { console.log('hello'); };
};

var p = new Person();

console.log(p instanceof Person);
console.log(typeof p.func);
p.func();
Tony
  • 7,345
  • 3
  • 26
  • 34
  • I think prototype is mentioned in the question, you're not using it at all and every instance of Person will have it's own version of func. Ineffective with resources if you create hundreds of complex object instances. About constructor functions and prototype: http://stackoverflow.com/a/16063711/1641941 – HMR Dec 07 '13 at 09:01