就是类似于electron 中的这种效果
mainWindow = new BrowserWindow({
    show: false,  // 在输入到 s 的时候就会自动提示 show 或其他 s 开头的属性
    width: 540,
    height: 540,
});
我在定义函数的时候指定了默认参数,好像可以实现,不知道是不是正规路数。。。。
var Test = function (cfg = { enabled: false }) {
}
利用结构函数写了一个构造函数,可以用,但是总感觉怪怪的,希望大神们指点
module.exports = function ({
    enabled = false,
    name = 'default',
    cname = '默认',
    init = () => { },
    restore = () => { }
} = {}) {
    // 感觉这里给this 赋值是不是有简单方法啊
    this.enabled = enabled;
    this.name = name;
    this.cname = cname;
    this.init = init;
    this.restore = restore;
    // 获取默认参数之外的参数
    if (arguments.length > 0) {
        var entry = [].shift.call(arguments);
        for (var i in entry) {
            if (!(i in this)) {
                this[i] = entry[i];
            }
        }
    }
    return this;
};
通过rest参数可以做到更简单一些
// 这里以一个构造函数为例
function Obj({p1='p1',p2=2,...others}={}){
    // function(p1='p1'){}  // 表示参数的默认值
    // function({p1,p2}={})  // 解构赋值
    // function(...p)  // rest参数,多个参数  
    // 这里还没想到更简单的方法
    this.p1=p1;
    this.p2=p2;
    // 默认参数之外的参数获取
    for(var i in others){
        this[i] = others[i];
    }
    return this;
}
大概就是这个样子吧,参考的这里
|  |      1codehz      2019-08-30 10:30:32 +08:00 用了 typescript,然后声明了参数的类型 | 
|  |      2iyeatse      2019-08-30 10:53:34 +08:00 纯 js 的话,可以试试 jsdoc https://jsdoc.app/tags-param.html | 
|  |      412tall OP @iyeatse 嗯嗯,这个感觉好理解啊,学习了,感谢!    ```js /** * Assign the project to an employee. * @param {Object} employee - The employee who is responsible for the project. * @param {string} employee.name - The name of the employee. * @param {string} employee.department - The employee's department. */ Project.prototype.assign = function({ name, department }) { // ... }; ``` |