简体   繁体   中英

Closure Compiler Error: JSC_NOT_A_CONSTRUCTOR

I was playing around with closure compiler and put in this code:

var obj = (function() {
  function H(a) {
    this.a = a
  }
  var h = new H(1);
  h.b=1
  return h
})();

I wanted to see if it would convert it to this:

var obj = (function() {
  function H(a) {
    this.a = a;
    this.b = 1
  }
  var h = new H(1);
  return h;
})();

But instead I got this error
JSC_NOT_A_CONSTRUCTOR: cannot instantiate non-constructor at line 6 character 8 var h = new H(1);

What am I doing wrong?

You have to tell CC that the function is a constructor via @constructor :

/**
 * Makes an H.
 * @constructor
 */
function H() {
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM