简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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