简体   繁体   English

如何开始使用 JavaScript 编码风格?

[英]How to start with a JavaScript coding style?

How can I learn or start learning the right coding style in JavaScript?我如何学习或开始学习 JavaScript 中正确的编码风格? What are my possibilities and what practical resources can I use to get used to the coding style?我有哪些可能性以及我可以使用哪些实用资源来习惯编码风格? Thanks!谢谢!

var getRequestUserList = function getRequestUserList(req,res,id) {
    User.findOne({_id: id}, function (err, user,next) {
    if (err) {
        console.log(err);
    }
    if(user){
        var userList = [];
        User.find(function (err, users) {
            if (err){
                console.log(err);
            }
            if(users){
                Friend.find({getReq:id,status:0 }, function (err, friends) {
                    if(err){
                        console.log(err);
                    }else{
                        for (var i = 0;i< users.length;i++){
                            for(var j = 0;j<friends.length;j++){
                                if(users[i]._id == friends[j].sendReq){
                                    userList.push({
                                        id : users[i]._id,
                                        name : users[i].name,
                                        surname : users[i].surname,
                                    });
                                }
                            }
                        }
                        res.json({friendUser:userList});

有像jshint.comstandardJS这样的代码样式,它们也包括自动代码格式化。

I recommend to install and use StandardJS .我建议安装和使用StandardJS It's an easy to use, hassle-free coding style.这是一种易于使用、轻松的编码风格。

Install it via:通过以下方式安装:

npm install standard --save-dev

Then run through the rules just to quickly get a feel of it.然后通过规则来快速了解它。

Finally, create a script in your package.json to run StandardJS, when you need it:最后,在您需要时在 package.json 中创建一个脚本来运行 StandardJS:

{
  "scripts": {
    "check": "standard"
  }
}

...then you can run it via npm run check ...然后你可以通过npm run check运行它


To provide a quick way to yourself to fix most coding style typos, add a fix script to your package.json :要为自己提供一种快速修复大多数编码风格拼写错误的方法,请在 package.json 中添加一个修复脚本

{
  "scripts": {
    "check": "standard",
    "fix": "standard --fix"
  }
}

...and run via npm run fix ...并通过npm run fix


To get a more nicer representation of coding style errors, install snazzy via npm install snazzy --save-dev , then modify your package.json like so:为了获得编码风格的错误的更多更好的代表性,安装时髦通过npm install snazzy --save-dev ,然后修改您的package.json像这样:

{
  "scripts": {
    "check": "standard --verbose | snazzy",
    "fix": "standard --fix"
  }
}

Just code as much as you can.尽可能多地编写代码。 After a while, you'll get the hang of the coding style of your choice.一段时间后,您将掌握所选择的编码风格。

Good luck, have fun!祝你好运,玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM