google / google/closure-compiler

Advanced compiled code works differently if referencing .constructor

Open
#4,216 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

The following code prints [123] in a console, the advanced compiled code prints [].
Compilation command: java -jar compiler.jar --language_out ECMASCRIPT5 --js 'test.js' --compilation_level=ADVANCED
Compiler version: v20240317

```
// test.js
const Foo = class {
/** @param {!Array=} opt_nums */
constructor(opt_nums){
/** @type {!Array} */
this.nums_ = opt_nums || [];
}

/** @param {number} num */
add(num){
this.nums_.push(num);
}

/** @return {!Foo} */
clone(){
return new this.constructor(this.nums_.slice(0));
}
}

const f = new Foo();
f.add(123);
console.log(f.clone().nums_);
```
Compiled code:
`function a(){this.g=[]}a.prototype.add=function(e){this.g.push(e)};var b=new a;b.add(123);var c=console,d=c.log,f;f=new b.constructor(b.g.slice(0));d.call(c,f.g);`

The opt_nums argument of a constructor is ignored in the compiled code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.