Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced compiled code works differently #4216

Open
liborek opened this issue Jan 30, 2025 · 1 comment
Open

Advanced compiled code works differently #4216

liborek opened this issue Jan 30, 2025 · 1 comment

Comments

@liborek
Copy link

liborek commented Jan 30, 2025

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<number>=} opt_nums */
	constructor(opt_nums){
		/** @type {!Array<number>} */
		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.

@concavelenz
Copy link
Contributor

concavelenz commented Jan 30, 2025

Yes, use the "constructor" property is considered reflection. If you would like to use it in that way you need to export or otherwise cause the constructor to "escape".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants