THRIFT-1044 Fix JavaScript inheritance 
Patch Wade Simmons


git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1062279 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index bbf436d..8d3c440 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -541,19 +541,12 @@
   out << "}\n";
 
   if (is_exception) {
-      if (gen_node_) {
-          out << "require('util').inherits(" <<
-              js_namespace(tstruct->get_program()) <<
-              tstruct->get_name() << ", Thrift.TException)" << endl;
-      } else {
-          out << "for (var property in Thrift.TException)"<<endl<<
-              js_namespace(tstruct->get_program())<<tstruct->get_name()<<"[property] = Thrift.TException[property]"<<endl;
-      }
-  }
-
-  if (!gen_node_) {
-      //init prototype
-      out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {}\n";
+    out << "Thrift.inherits(" <<
+        js_namespace(tstruct->get_program()) <<
+        tstruct->get_name() << ", Thrift.TException)" << endl;
+  } else {
+    //init prototype
+    out << js_namespace(tstruct->get_program())<<tstruct->get_name() <<".prototype = {}\n";
   }
 
 
@@ -935,17 +928,15 @@
 
 
   if (tservice->get_extends() != NULL) {
-      extends = tservice->get_extends()->get_name();
-
-      f_service_ << "for (var property in "<<extends<<"Client)"<<endl<<
-          js_namespace(tservice->get_program()) << service_name_<<"Client[property] = "<<extends<<"Client[property]"<<endl;
-
+    indent(f_service_) << "Thrift.inherits(" <<
+        js_namespace(tservice->get_program()) <<
+        service_name_ << "Client, " <<
+        tservice->get_extends()->get_name() << ".Client)" << endl;
+  } else {
+      //init prototype
+      indent(f_service_) <<  js_namespace(tservice->get_program())<<service_name_ << "Client.prototype = {}"<<endl;
   }
 
-  //init prototype
-  f_service_ <<  js_namespace(tservice->get_program())<<service_name_ << "Client.prototype = {}"<<endl;
-
-
   // Generate client method implementations
   vector<t_function*> functions = tservice->get_functions();
   vector<t_function*>::const_iterator f_iter;
diff --git a/lib/js/thrift.js b/lib/js/thrift.js
index fbdc809..c06e27a 100644
--- a/lib/js/thrift.js
+++ b/lib/js/thrift.js
@@ -723,4 +723,10 @@
   return length;
 }
 
-
+Thirft.inherits = function(constructor, superConstructor) {
+  // Prototypal Inheritance
+  // http://javascript.crockford.com/prototypal.html
+  function F() {}
+  F.prototype = superConstructor.prototype;
+  constructor.prototype = new F();
+}
diff --git a/lib/nodejs/lib/thrift/thrift.js b/lib/nodejs/lib/thrift/thrift.js
index aee5a54..87acc6f 100644
--- a/lib/nodejs/lib/thrift/thrift.js
+++ b/lib/nodejs/lib/thrift/thrift.js
@@ -132,3 +132,7 @@
 exports.objectLength = function(obj) {
   return Object.keys(obj).length;
 }
+
+exports.inherits = function(constructor, superConstructor) {
+  sys.inherits(constructor, superConstructor);
+}