简体   繁体   中英

Closure Compiler Ignores Object Mutation

I put the following code into closure compiler on advanced mode:

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

The result I get back is:

(function() {
  var a = new function() {
  }(1);
  a.a = 1;
  return a;
})();

Why is it ignoring the change I make to the object hb=1 ?

The advanced compilation options enable aggressive property removal , which includes some assumptions:

It makes a strong assumption that properties defined on a "prototype" or "this" will not be iterated over and thus is a candidate for removal.

/** @constructor */ function cls() { this.x = 1; // removal candidate due to "this" assumption; }

So what you're seeing is actually this.a = a being removed, and then the property b is being renamed to a .

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