5

I found this. What does it do?

function G(a, b) {
  var c = function() { };
  c.prototype = b.prototype;
  a.T = b.prototype;
  a.prototype = new c;
}
Community
  • 1
  • 1
TJR
  • 3,617
  • 8
  • 38
  • 41
  • 1
    I guess what you're asking is "why would anyone do this?", because other than answering "to confuse people", I don't have anything for you. – wlangstroth May 20 '10 at 14:56
  • 1
    Where did you find this? The `T` property being set is not typical of any pattern I have seen regarding prototypical inheritance. But what I believe this might be is having the constructor inherited as well as the prototype chain. – Sean Kinsey May 20 '10 at 15:00

2 Answers2

1

It looks similar to the Crockford's Object.create method, but this function is used to "setup" constructors.

It accepts two constructors as arguments, and it setups the prototype of the first one.

Let me rename the cryptic variable names:

function G(sub, super) {
  var F = function() { };
  F.prototype = super.prototype;
  sub.superLink = super.prototype;
  sub.prototype = new F();
}

function Super () {
  //...
}
Super.prototype.member1 = 'superMember1';

function Sub() {
  this.member2 = 'subMember2';
}

G(Sub, Super);

new Sub(); // Object { member2="subMember2",  member1="superMember1"}

Edit: The T property is simply used to know what is the "super" constructor of the sub one, I've seen this pattern on other places, like in the book Pro JavaScript Design Patterns (page 43), with some additions, to prevent the constructor property to point to the wrong object:

function extend(subClass, superClass) {
    var F = function() {};
    F.prototype = superClass.prototype;
    subClass.prototype = new F();
    subClass.prototype.constructor = subClass;

    subClass.superclass = superClass.prototype;
    if(superClass.prototype.constructor == Object.prototype.constructor) {
        superClass.prototype.constructor = superClass;
    }
}

See also:

Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    I don't think its that simple, the `T` property is used in some way not clear by the question. Also, the result from your answer can be reached by just setting `Sub.prototype = Super.prototype` – Sean Kinsey May 20 '10 at 14:59
  • 1
    @Sean Not quite. By doing that, if you add a member to `Sub.prototype` you will also add a member to `Super.prototype`. – Pablo Cabrera May 20 '10 at 15:17
  • @CMS I think it is worth to note that `member2` will be on the prototype chain of `new Sub()` and not on `new Sub()` itself. – Pablo Cabrera May 20 '10 at 15:18
  • @Pablo, not quite, `member2` is an own property, e.g. `new Sub().hasOwnProperty('member2'); // true` – Christian C. Salvadó May 20 '10 at 15:39
  • @Sean, the purpose of `T` is for *knowing* from which constructor another was extended, check this pattern [here](http://books.google.com/books?id=za3vlnlWxb0C&lpg=PP1&dq=Pro%20JavaScript%20Design%20Patterns&pg=PA43#v=onepage&q&f=false). – Christian C. Salvadó May 20 '10 at 16:02
  • @CMS Thats what I thought from the start. But the `G` function does not support subclasses with a separate prototype, so I fail to see the usecase for this method, it pretty much just copies the prototype from one function to another.. – Sean Kinsey May 20 '10 at 16:30
  • @CMS Oh sorry, I meant `member1` from `Super.prototype` – Pablo Cabrera May 20 '10 at 16:36
0

it looks like it gives you the ability to pass in a function, make changes to it, and then pass it back - a.prototype is a new instantiated copy of it. There are examples on the net using this type of code for "getting" and "setting".

JasonMichael
  • 2,463
  • 4
  • 26
  • 25