V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
12tall
V2EX  ›  JavaScript

请问 vscode 中 js 中将对象传递给函数的时候,自动提示对象属性的名称是怎么做到的

  •  
  •   12tall · 2019-08-30 10:27:47 +08:00 · 3207 次点击
    这是一个创建于 1672 天前的主题,其中的信息可能已经有所发展或是发生改变。

    就是类似于electron 中的这种效果

    mainWindow = new BrowserWindow({
        show: false,  // 在输入到 s 的时候就会自动提示 show 或其他 s 开头的属性
        width: 540,
        height: 540,
    });
    

    我在定义函数的时候指定了默认参数,好像可以实现,不知道是不是正规路数。。。。

    var Test = function (cfg = { enabled: false }) {
    
    }
    
    第 1 条附言  ·  2019-08-30 11:15:07 +08:00
    感谢回复,最好的答案应该是 es6 的解构了吧
    第 2 条附言  ·  2019-08-30 14:09:58 +08:00

    利用结构函数写了一个构造函数,可以用,但是总感觉怪怪的,希望大神们指点

    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;
    };
    
    第 3 条附言  ·  2019-09-03 10:54:20 +08:00

    通过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;
    }
    

    大概就是这个样子吧,参考的这里

    5 条回复    2019-08-30 11:22:53 +08:00
    codehz
        1
    codehz  
       2019-08-30 10:30:32 +08:00
    用了 typescript,然后声明了参数的类型
    iyeatse
        2
    iyeatse  
       2019-08-30 10:53:34 +08:00
    纯 js 的话,可以试试 jsdoc
    https://jsdoc.app/tags-param.html
    12tall
        3
    12tall  
    OP
       2019-08-30 11:04:26 +08:00
    @codehz 谢谢,学习下
    12tall
        4
    12tall  
    OP
       2019-08-30 11:12:30 +08:00
    @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 }) {
    // ...
    };
    ```
    lllllliu
        5
    lllllliu  
       2019-08-30 11:22:53 +08:00
    @12tall 还阔以支持一键生 doc
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2957 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:32 · PVG 08:32 · LAX 17:32 · JFK 20:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.