Home:ALL Converter>What does this do?

What does this do?

Ask Time:2010-05-20T22:45:00         Author:TJR

Json Formatter

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;
}

Author:TJR,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2874816/what-does-this-do
Christian C. Salvadó :

It looks similar to the Crockford's Object.create method, but this function is used to \"setup\" constructors.\n\nIt accepts two constructors as arguments, and it setups the prototype of the first one.\n\nLet me rename the cryptic variable names:\n\nfunction G(sub, super) {\n var F = function() { };\n F.prototype = super.prototype;\n sub.superLink = super.prototype;\n sub.prototype = new F();\n}\n\nfunction Super () {\n //...\n}\nSuper.prototype.member1 = 'superMember1';\n\nfunction Sub() {\n this.member2 = 'subMember2';\n}\n\nG(Sub, Super);\n\nnew Sub(); // Object { member2=\"subMember2\", member1=\"superMember1\"}\n\n\nEdit: 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:\n\nfunction extend(subClass, superClass) {\n var F = function() {};\n F.prototype = superClass.prototype;\n subClass.prototype = new F();\n subClass.prototype.constructor = subClass;\n\n subClass.superclass = superClass.prototype;\n if(superClass.prototype.constructor == Object.prototype.constructor) {\n superClass.prototype.constructor = superClass;\n }\n}\n\n\nSee also: \n\n\nJavaScript inheritance extend function\n",
2010-05-20T14:55:32
yy