简体   繁体   中英

whats is underscore meaning before a function js or php?

I need to know what is underscore ( _ ) mean if its write before function, variable, is for just describe something, or its needed to do or execute some of calling function ..etc

JS

var _initMobile  ... //variable 
_addEvent(documentElement, [EVENT_TOUCHSTART ...) //event handler

PHP

function _getBackLink(&$node, $uri, $title) {}...// php function

In fact, I did not know how to look for it..ٍSo Im ask

Thats only a coding convention. These languages does not interpret the underscore in any way. In most cases thats the way developers use to "mark" the function as private.

It's good to go by conventions . Why do you make SITE_NAME in caps? Because it's global. Same is here.

In JavaScript:

  • It means convention for private fields or private methods. Methods that are only for internal use. They should not be invoked outside of the class.

  • Private fields contain data for internal use. They should not be read or written into (directly) from outside of the class.

Great answer here about PHP:

It's from the bad old days of Object Oriented PHP (PHP 4). That implementation of OO was pretty bad, and didn't include things like private methods. To compensate, PHP developers prefaced methods that were intended to be private with an underscore. In some older classes you'll see /**private*/ __foo() { to give it some extra weight.

I've never heard of developers prefacing all their methods with underscores, so I can't begin to explain what causes that.

Its just a convention for readability IMO. No access restrictions are imposed by both the languages when a variable or function contains a underscore before it.

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