gpt4 book ai didi

javascript - 如何在 Closure Compiler 的 javascript 中注释嵌套对象并使所有属性都是可选的?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:15 26 4
gpt4 key购买 nike

这是我的 A 类,我希望所有选项都是可选的。它适用于 a、b、c 属性,但不适用于 c.cX 属性。如何正确地使所有属性成为可选的?

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

/**
* @typedef {{
* a: (string|undefined),
* b: (number|undefined),
* c: ({
* ca: (string|undefined),
* cb: (number|undefined),
* cc: (Function|undefined)
* }|undefined)
* }}
*/
var Options;


/**
* @param {Options=} options
* @constructor
*/
var A = function(options) {
console.log(this);
};


new A({
a: 'x',
c: {
ca: 'x',
//cb: 1,
cc: function() {}
}
});

最佳答案

Options 类型应该这样定义:

/**
* @record
* @struct
*/
function Options() {};

/** @type {string|undefined} */
Options.prototype.a;

/** @type {number|undefined} */
Options.prototype.b;

/** @type {!OptionsC|undefined} */
Options.prototype.c;


/**
* @record
* @struct
*/
function OptionsC() {};

/** @type {string|undefined} */
OptionsC.prototype.ca;

/** @type {number|undefined} */
OptionsC.prototype.cb;

/** @type {Function|undefined} */
OptionsC.prototype.cc;

关于javascript - 如何在 Closure Compiler 的 javascript 中注释嵌套对象并使所有属性都是可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com