简体   繁体   English

在 JavaScript 中是否有重命名 document.querySelector 等的约定?

[英]Is there a convention for renaming document.querySelector etc in JavaScript?

In the unlikely event that I am万一我是

  1. writing JavaScript写作 JavaScript
  2. don't want to use a framework like jQuery不想使用像 jQuery 这样的框架
  3. want to replace frequent calls to 'document.querySelector' with something shorter想要用更短的内容替换对“document.querySelector”的频繁调用
  4. want my code to still be readable and familiar to new developers who look at it希望我的代码对于查看它的新开发人员来说仍然是可读和熟悉的

Are there any abbreviations that are commonly used to replace querySelector, querySelectorAll, etc?是否有任何常用的缩写来代替 querySelector、querySelectorAll 等?

This is a question about naming conventions.这是一个关于命名约定的问题。 I'm not asking how to abbreviate a long function name .我不是在问如何缩写一个长的 function 名称 For reference, you can do it like this:作为参考,您可以这样做:

 var queryAll = document.querySelectorAll.bind(document);

No, there isn't.不,没有。

Furthermore, I don't think its a good idea to use abbreviations.此外,我认为使用缩写不是一个好主意。 Why change the Javascript API for no other reason than to make it shorter?为什么要更改 Javascript API 只是为了使其更短?

You could reuse something like $ , but then you might give the reader the impression that you are using an existing library that uses that abbreviation.您可以重复使用$ ,但这样您可能会给读者留下您正在使用使用该缩写的现有库的印象。

Some other considerations:其他一些考虑:

  • What happens if you later decide to add a library that uses the same abbreviation?如果您稍后决定添加使用相同缩写的库,会发生什么情况? You need to refactor your code你需要重构你的代码
  • What happens if someone wants to reuse some of your code and your abbreviations conflict with there code or libraries.如果有人想重用您的某些代码并且您的缩写与那里的代码或库发生冲突,会发生什么情况。
  • With modern editors / IDEs, using abbreviations will not likely reduce the amount of typing if you are using auto fill使用现代编辑器/IDE,如果您使用自动填充,则使用缩写不太可能减少打字量
  • For someone learning Javascript it will most likely make make your source confusing to read.对于学习 Javascript 的人来说,它很可能会使您的源代码难以阅读。

I've done this in the past only to regret it later.我过去做过这件事,后来才后悔。 Makes your code less portable.使您的代码不那么可移植。

Short answer.简短的回答。 I don't recommend it.我不推荐它。

not that I know of从来没听说过

Let's make some让我们做一些

Modern JS oneliner usable in all modern browsers现代 JS oneliner 可用于所有现代浏览器

Selector = (s,c) => (c ?? document).querySelectorAll(s);

Usage用法

// search in the whole document
Selector('.some, .randomClasses');

// search only in the context of someParent
var someParent = Selector('.give .me .parent')[0];
Selector('img', someParent);

Leverages arrow function expression and Nullish coalescing operator (??) and grouping operator ()利用箭头函数表达式空合并运算符 (??)分组运算符 ()
Unless not obvious, this always returns the same stuff, querySelectorAll would, you can easily adjust this to do similar with querySelector maybe naming it eg SelectorOne , you get the picture...除非不明显,否则这总是返回相同的东西, querySelectorAll会,您可以轻松调整它以与querySelector类似,也许将其命名为SelectorOne ,您会得到图片...

More robust JS with compatibility check具有兼容性检查的更强大的 JS

Usage stays the same, but in case someone's worried about it breaking stuff:用法保持不变,但万一有人担心它会破坏东西:

if (typeof Selector != "undefined") {
  console.warn('CONFLICT WITH Selector! - please address this issue!')
} else {
  function Selector(selector, current_context) {
    if (typeof current_context == "undefined") {
      current_context = document;
    }

    return current_context.querySelectorAll(selector);
  }
}

Why I chose naming "Selector"为什么我选择命名“选择器”

  • I chose the naming with uppercase 1st letter on purpose , since interfaces are usually named that way and it slightly decreases risk of conflicting variable for a piggy code on global level选择了与目的大写第一个字母命名,因为接口通常命名的方式,它略微降低了在全球范围内,小猪码冲突的变量的风险
  • I didn't name it没有命名Select because that is ambiguous and could imply Selection which is a different experimental interface因为这是模棱两可的,并且可能暗示Selection这是一个不同的实验界面
  • Selector , in HTML&CSS world, is specific enough :) Selector ,在 HTML&CSS 世界中,已经足够具体了 :)


PS: I really do wonder why they made it so long, when that was the main reason people were using heavy libraries (like old jQuery) instead of doing it in plain JavaScript... I really do hope it was a technical reason... PS:我真的很想知道为什么他们把它弄这么久,当这是人们使用繁重的库(比如旧的 jQuery)而不是用纯 JavaScript 来做的主要原因时......我真的希望这是一个技术原因.. .

While there isn't any convention, one other solution that I haven't seen mentioned here would be to declare虽然没有任何约定,但我在这里没有提到的另一种解决方案是声明

const querySelector = (sel, el) => (el ?? document).querySelector(sel);
const querySelectorAll = (sel, el) => (el ?? document).querySelectorAll(sel);

which is inspired from @jave.web's answer in syntax.灵感来自@jave.web 的语法回答。

The advantages are that this is:优点是:

  • shorter because you get to ditch document.更短,因为您可以放弃document. in case you're selecting from it;如果您要从中选择;
  • still completely intelligible for outsiders reading your code.对于阅读您的代码的外人来说仍然是完全可以理解的。 A google search for querySelector will immediately yield results, unlike Selector .Selector不同,谷歌搜索querySelector将立即产生结果。

Also, as Steve said, modern IDEs will autocomplete the name anyways so there no need to make it any shorter.此外,正如史蒂夫所说,现代 IDE 无论如何都会自动完成名称,因此无需缩短名称。 Instead of typing doc "tab" .qu "tab" you'll be just typing qu "tab".不用输入doc "tab" .qu "tab",你只需输入qu "tab"。 To me, this seems reasonable.在我看来,这似乎是合理的。

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

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