google / google/closure-compiler

Class' static methods are placed on a totally different object, but static properties are not

Open
#2,474 3 comments 0 reactions 0 assignees View on GitHub
P3
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

The entirety of my input is a class, `ClassA`, with one static property and one static method. The end goal is to make sure neither of these get renamed. I also attach `ClassA` to `myLib`, which I attach to the `window`.

So ideally I can call either of these:

```js
myLib.ClassA.StaticProperty
myLib.ClassA.StaticMethod()
```

When I try it, the property works but not the method:

```JavaScript
> myLib.ClassA.StaticProperty
"static prop value"
> myLib.ClassA.StaticMethod()
VM721:1 Uncaught TypeError: myLib.ClassA.StaticMethod is not a function
at :1:14
```

Where's the method? Instead of being placed on the `myLib.ClassA` class, its placed on a newly created object, `window.classA`!

```JavaScript
> window.ClassA.StaticMethod()
"static method value"
```

This seems like a bug. The only files I have are the one `index.js` file and `base.js` from the Closure library. Input and output are below. Is there anything else I can give you to be helpful here?

```
java -jar ./closure-compiler.jar --js "tsout/**.js" --js_output_file=lib.js --compilation_level ADVANCED_OPTIMIZATIONS --formatting=pretty_print --export_local
_property_definitions=true --generate_exports --language_out=ECMASCRIPT5
```

```JavaScript
// index.js, my input:
/**
* @class
*/
class ClassA {
constructor() {
this.myProp = 'hello';
}

/**
* @export
* @static
*/
static StaticMethod() { return "static method value"; };
/**
* @export
* @static
*/
static get StaticProperty() { return "static prop value"; };
}

/**
* @export
*/
let myLib = {};
// very semantic version
myLib['version'] = "lalala";
// runtime API
((window))['myLib'] = myLib;
myLib['ClassA'] = ClassA;
```

```JavaScript
// closure output:
var d = "undefined" != typeof window && window === this ? this : "undefined" != typeof global && null != global ? global : this, e = this;
function g(b, f) {
b = b.split(".");
var a = e;
b[0] in a || !a.execScript || a.execScript("var " + b[0]);
for (var c; b.length && (c = b.shift());) {
b.length || void 0 === f ? a[c] && a[c] !== Object.prototype[c] ? a = a[c] : a = a[c] = {} : a[c] = f;
}
}
;function h() {
}
g("ClassA.StaticMethod", function() {
return "static method value";
});
d.Object.defineProperties(h, {StaticProperty:{configurable:!0, enumerable:!0, get:function() {
return "static prop value";
}}});
var k = {};
g("myLib", k);
k.version = "lalala";
window.myLib = k;
k.ClassA = h;
```

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.