@@ -12,6 +12,7 @@ var Type = protobuf.Type,
1212 Service = protobuf . Service ,
1313 Enum = protobuf . Enum ,
1414 Namespace = protobuf . Namespace ,
15+ Class = protobuf . Class ,
1516 util = protobuf . util ;
1617
1718var out = [ ] ;
@@ -150,7 +151,8 @@ var shortVars = {
150151 "f" : "impl" ,
151152 "o" : "options" ,
152153 "d" : "object" ,
153- "n" : "long"
154+ "n" : "long" ,
155+ "p" : "properties"
154156} ;
155157
156158function beautifyCode ( code ) {
@@ -214,7 +216,8 @@ function buildFunction(type, functionName, gen, scope) {
214216
215217 code = code . replace ( / { 4 } / g, "\t" ) ;
216218
217- var hasScope = scope && Object . keys ( scope ) . length ;
219+ var hasScope = scope && Object . keys ( scope ) . length ,
220+ isCtor = functionName === type . name ;
218221
219222 if ( hasScope ) // remove unused scope vars
220223 Object . keys ( scope ) . forEach ( function ( key ) {
@@ -223,7 +226,9 @@ function buildFunction(type, functionName, gen, scope) {
223226 } ) ;
224227
225228 var lines = code . split ( / \n / g) ;
226- if ( hasScope ) // enclose in an iife
229+ if ( isCtor ) // constructor
230+ push ( lines [ 0 ] ) ;
231+ else if ( hasScope ) // enclose in an iife
227232 push ( name ( type . name ) + "." + functionName + " = (function(" + Object . keys ( scope ) . join ( ", " ) + ") { return " + lines [ 0 ] ) ;
228233 else
229234 push ( name ( type . name ) + "." + functionName + " = " + lines [ 0 ] ) ;
@@ -235,7 +240,9 @@ function buildFunction(type, functionName, gen, scope) {
235240 push ( line . trim ( ) ) ;
236241 indent = prev ;
237242 } ) ;
238- if ( hasScope )
243+ if ( isCtor )
244+ push ( "}" ) ;
245+ else if ( hasScope )
239246 push ( "};})(" + Object . keys ( scope ) . map ( function ( key ) { return scope [ key ] ; } ) . join ( ", " ) + ");" ) ;
240247 else
241248 push ( "};" ) ;
@@ -281,19 +288,9 @@ function buildType(ref, type) {
281288 type . comment ? "@classdesc " + type . comment : null ,
282289 "@exports " + fullName ,
283290 "@constructor" ,
284- "@param {Object} [properties] Properties to set"
291+ "@param {Object} [" + ( config . beautify ? " properties" : "p" ) + " ] Properties to set"
285292 ] ) ;
286- push ( "function " + name ( type . name ) + "(properties) {" ) ;
287- ++ indent ;
288- push ( "if (properties)" ) ;
289- ++ indent ;
290- push ( "for (" + ( config . es6 ? "let" : "var" ) + " keys = Object.keys(properties), i = 0; i < keys.length; ++i)" ) ;
291- ++ indent ;
292- push ( "this[keys[i]] = properties[keys[i]];" ) ;
293- -- indent ;
294- -- indent ;
295- -- indent ;
296- push ( "}" ) ;
293+ buildFunction ( type , type . name , Class . generate ( type ) ) ;
297294
298295 // default values
299296 var firstField = true ;
0 commit comments