Вопрос

In the data that I send to a Google Closure template, I have a property named default. I plan on compiling both the JavaScript code and the code generated by SoyToJsSrcCompiler using the Google Closure Compiler. But the problem is that the generated code from the template prevents the compiler from renaming the default property.

This is the template code:

/**
 * @param test
 */
{template .template}
    <div>{$test.a.b.default.c.d}</div>
{/template}

And this is the generated code:

/**
 * @param {Object.<string, *>=} opt_data
 * @param {(null|undefined)=} opt_ignored
 * @return {string}
 * @notypecheck
 */
test.template = function(opt_data, opt_ignored) {
  return '<div>' + soy.$$escapeHtml(opt_data.test.a.b['default'].c.d) + '</div>';
};

Is there any way I can get:

opt_data.test.a.b.default.c.d

intead of:

opt_data.test.a.b['default'].c.d

or any other way in which I can keep my property named default?

Right now, the compiler renames the default property in my JavaScript code, but doesn't rename it in the code generated by SoyToJsSrcCompiler, because this code uses the quoted version.

Это было полезно?

Решение 2

There doesn't currently seem to be any supported way of doing this. I changed the name of the property from default to something else that gets renamed properly.

Другие советы

default is a semi reserved word in JavaScript. It is used in switch case statements:

switch ( variable ) {
    case a:
        break;
    case b:
        break;
    default:
        break;
}

It it perfectly safe to in object traveling but not as scope level variable:

object.some.thing.else.default;

Consider this object tree:

var a = {
    b: {
        c: {
            "1": {
                "default": 2
            } 
        }
    }
};

You can get the value (2) by saying:

a.b.c[1].default // 2

But also:

a["b"]["c"]["1"]["default"] // 2

JSHint throws a warning for quoting object traveling for non qoute needed situations. In this case ["b"], ["c"], ["default"].

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top