Pergunta

/**
 * @param {Object} object
 * @param {(string|number)} name
 * @param {*} value
 */
var fabric = function(object, name, value) {
    object[name] = value;
};


fabric(Number, 'MAX_INTEGER', 9007199254740991);
// ...

console.log(Number.MAX_INTEGER); // 9007199254740991

WARNING: JSC_INEXISTENT_PROPERTY: Property MAX_INTEGER never defined on Number at line 14 character 12

How to declare the dynamic properties without pre definitions?

UPD:

Number['MAX_INTEGER'];

Foi útil?

Solução

This falls under the Restrictions for ADVANCED_OPTIMIZATIONS of the documentation. You must consistently refer to properties using either the dotted notation or quoted syntax. When you mix access, the compiler may rename the dotted access, but will not touch the quoted syntax and thus generate incorrect code.

Outras dicas

If you do want to add properties in this way, you have two alternatives:

add a stub declaration in your externs (which will prevent renaming):

/** @const {number} */
Number.MAX_INTEGER;

or use @lends with a object literal:

/**
 * @param {Object} object
 * @param {Object} props
 */
var fabric2 = function(object, props) {
  for (var prop in props) {
    object[prop] = props[prop];
  }
};

fabric2(Number, /** @lends {Number} */ { MAX_INTEGER: 9007199254740991 });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top