HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

Constructor prototype breaks with --use_only_custom_externs and --use_types_for_optimization #1208

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the product are you using? On what operating system?
Closure Compiler (http://code.google.com/closure/compiler)
Version: v20140110
Built on: 2014/01/14 16:25

Debian GNU/Linux Xfce

Please provide any additional information below.
Compile this program with ADVANCED_OPTIMIZATIONS, --use_only_custom_externs, 
and --use_types_for_optimization:

  /**
   * @constructor
   */
  function Foo() {}
  Foo.prototype.bar = function () {
    this.bar = 1
  }
  new Foo().bar()

The output is this, which is incorrect:

  function a(){}a.a.a=function(){this.a=1};(new a).a();

If you remove either --use_only_custom_externs or --use_types_for_optimization 
it then gives the correct output:

  function a(){}a.prototype.a=function(){this.a=1};(new a).a();

And you can't work around it by quoting the "prototype" property, like this:

  /**
   * @constructor
   */
  function Foo() {}
  Foo["prototype"].bar = function () {
    this.bar = 1
  }
  new Foo().bar()

Because now the compiler gives the warning "dangerous use of the global this 
object".

I see two solutions:

1) Change the compiler so it recognizes ["prototype"] of constructors.

2) Change the compiler so it recognizes .prototype of constructors even when 
both --use_only_custom_externs and --use_types_for_optimization are set.

Either solution is acceptable for me, though the second one is more convenient 
to write.

Original issue reported on code.google.com by pcxunlimited@gmail.com on 24 Jan 2014 at 9:54

GoogleCodeExporter commented 9 years ago
As with your other issue, the compiler is only designed to work with externs. 
If you utilize the --use_only_custom_externs flag, it is your responsibility to 
provide the externs for the built-in objects. This functionality exists so you 
can override the definitions of the externs - not to completely omit them.

Original comment by chadkill...@missouristate.edu on 24 Jan 2014 at 10:38