The $ dollar sign

后端 未结 9 1778
执笔经年
执笔经年 2020-11-29 04:16

I have something simple like this:

$(selector).append("somestuff");

But since I\'m going to reuse the selector I cache it with:

相关标签:
9条回答
  • 2020-11-29 04:41

    In this case, $selector is an example of Hungarian notation. It is there so that you know that $selector is a jQuery object.

    If you make it your custom to always have jQuery objects start with $ if you suddenly use one wrong it will look wrong which will help you find a problem in your code.

    The language couldn't care less. They could have just as well named the jQuery function J or anything else; the $ is not a special symbol.

    0 讨论(0)
  • 2020-11-29 04:43

    The cash sign is just an alias for the jQuery function. Starting the variable name with $ has no effect on that variable.

    It is customary though, especially in jQuery plugin development, to use variables already wrapped in jQuery with a cash sign, so you know you can call jQuery methods, without having to wrap them.

    0 讨论(0)
  • 2020-11-29 04:44

    Yeah.

    jQuery always returns a jQuery object. It's what allows chaining.

    You've already defined the variable so you get a jQuery object back so $ is unnecessary

    0 讨论(0)
提交回复
热议问题