thanhlong203 / closure-library

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

Compiler should warn/error when instance methods are operated on #317

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Compile and run the following code:
  goog.require('goog.graphics.Path');
  function demo() {
    var path = new goog.graphics.Path();
    var points = [[1,1], [2,2]];
    for (var i = 0; i < points.length; i++) {
      (i == 0 ? path.moveTo : path.lineTo)(points[i][0], points[i][1]);
    }
  }
  goog.exportSymbol('demo', demo);

What is the expected output? What do you see instead?
I expect it to either work or produce a warning.  In this case, the latter 
since there's an error in the javascript - when calling path.moveTo(x, y), 
"this" is set correctly to the path element in the moveTo function.  But when 
the function is operated on, as in (i == 0 ? path.moveTo : path.lineTo)(x, y), 
it's no longer an instance method invocation, so "this" reverts to the window 
object.  In this case, an error results because moveTo references a field in 
Path that is now "undefined".  Better would be to issue a warning/error that an 
instance method is being converted to a normal function (perhaps only if it 
references this).

What version of the product are you using? On what operating system?
Unknown (it's built into my build tools) - I presume this issue is present in 
all builds.  Running on ubuntu.

Please provide any additional information below.

Original issue reported on code.google.com by s...@google.com on 29 Apr 2011 at 9:19

GoogleCodeExporter commented 8 years ago
Please file this feature request at the closure-compiler project.

Original comment by pall...@google.com on 3 May 2011 at 5:11