google / google/closure-compiler

goog.reflect.objectProperty returning wrong name

Open
#3,709 4 comments 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

A disclaimer: I'm new to Closure Compiler and I know nothing about Closure Library, but I stumbled upon [this StackOverflow question](https://stackoverflow.com/questions/41832887/is-it-possible-to-get-the-compiled-function-name-by-from-google-closure-from-wit). It seemed like using `goog.reflect.objectProperty` would help me solve my problem, where I need to have a particular property name available as a string in the code. Why, you might ask? Well, because I have access to the `setInterval` function that can only accept string, so I need to know what to call it with.

I cannot include Closure Library in what I'm working at, but it seems it's enough to do
`var goog = { reflect: { objectProperty: function(name, obj) { return name; } } };`
in the code to make it work. I'm not sure how it works, but I suppose this function is hard-coded in the compiler even without loading the library itself.

Enough with the background.
```
java -jar closure-compiler-v20201006.jar --js a.js -O ADVANCED --language_in ECMASCRIPT3 --language_out ECMASCRIPT3
```
This doesn't work with this code in a.js:
```javascript
var goog = { reflect: { objectProperty: function(name, obj) { return name; } } };

var CoolNamespace = {
fun: function() {
alert('it worked');
}
};

CoolNamespace.fun();
setTimeout(goog.reflect.objectProperty('CoolNamespace', this) + '.' + goog.reflect.objectProperty('fun', CoolNamespace) + '()', 1000);
```
because it returns:
```javascript
var a={a:function(){alert("it worked")}};a.a();setTimeout("b.a()",1E3);
```
which is obviously wrong. We should get `a.a()` in the string.

What's even more funny to me is what happens when you delete this line from the source:
```javascript
CoolNamespace.fun();
```
Then the resulting code is:
```javascript
setTimeout("b.a()",1E3);
```
I kinda know what happens here, but I feel that this is still wrong: I might say I got a wrong thing out of `goog.reflect.objectProperty` call, because there is no `b` or `a` anywhere.

As I said, I'm very new to all this, but I hope this is real issue and not me misunderstanding something I don't fully comprehend yet.

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.