Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

javascript - 有人可以用Javascript解释美元符号吗?(Can someone explain the dollar sign in Javascript?)

The code in question is here:

(有问题的代码在这里:)

var $item = $(this).parent().parent().find('input');

What is the purpose of the dollar sign in the variable name, why not just exclude it?

(变量名中美元符号的目的是什么,为什么不排除呢?)

  ask by Keith Donegan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A '$' in a variable means nothing special to the interpreter, much like an underscore.

(变量中的'$'对解释器来说没有什么特别之处,就像下划线一样。)

From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.

(从我所看到的,很多人使用jQuery (这是你的示例代码对我来说)倾向于为包含带有$的jQuery对象的变量添加前缀,以便它们易于识别并且不会与整数相混淆。)

The dollar sign function $() in jQuery is a library function that is frequently used, so a short name is desirable.

(jQuery中的美元符号函数$()是一个经常使用的库函数,因此需要一个简短的名称。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...