最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why sometimes in Javascript there is the dollar sign ($arg) before function argument? - Stack Overflow

programmeradmin0浏览0评论

I see sometimes js snippets that are using the $ in front of the argument ($argName).

function someName(arg) {
  // some code
  // using arg
}

function someName($arg) {
  // some code
  // using $arg
}

In this js example it works either way with or without the $ in front of the arguments. Can anyone explaine if it has any use?

I see sometimes js snippets that are using the $ in front of the argument ($argName).

function someName(arg) {
  // some code
  // using arg
}

function someName($arg) {
  // some code
  // using $arg
}

In this js example it works either way with or without the $ in front of the arguments. Can anyone explaine if it has any use?

Share Improve this question edited Oct 11, 2016 at 18:37 CerebralFart 3,4905 gold badges28 silver badges31 bronze badges asked Mar 19, 2015 at 14:13 Giorgio25bGiorgio25b 4165 silver badges13 bronze badges 6
  • 1 The "$" character is just a character that can be used in an identifier - there's nothing special about it. – Pointy Commented Mar 19, 2015 at 14:14
  • 3 some people use it as a convention to name a jQuery element or an angular builtin service. – Daniel A. White Commented Mar 19, 2015 at 14:15
  • Hungarian notation why people only use it for jQuery object drives me mad. – epascarello Commented Mar 19, 2015 at 14:25
  • @epascarello I find it useful as it reminds colleagues that they needn't rewrap the parameter in another $() call. It's a long way from full MS-style Hungarian notation IMHO, and more useful given that JS isn't a strongly typed language. – Alnitak Commented Mar 19, 2015 at 14:49
  • @Alnitak JSDoc or any other code documentation seems to fix that problem without making a special case exception for one variable type. – epascarello Commented Mar 19, 2015 at 15:30
 |  Show 1 more ment

2 Answers 2

Reset to default 8

The $ character is legal in JS identifiers, and is often used simply as a code convention to indicate that the passed parameter is already a jQuery object (as opposed to a native DOM element).

This serves as a reminder that it's not necessary to re-invoke the jQuery $(param) wrapper function on that parameter.

It's also used quite a lot in Angular JS code.

It's sometimes used to reference an object from another library , like JQuery or AngularJS , what you're talking about here looks like AngularJs's dependency injection to me

UPDATE

See this answer it might be useful

发布评论

评论列表(0)

  1. 暂无评论