3

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

Anees Hikmat Abu Hmiad
  • 3,460
  • 2
  • 19
  • 36
  • 3
    Look [Here](http://stackoverflow.com/questions/663350/whats-the-deal-with-a-leading-underscore-in-php-class-methods) – Iralution Jul 08 '14 at 09:57
  • 1
    In php its the old school way to indicate that a method is protected. Not sure if it has actual meaning in js – Anthony Jul 08 '14 at 09:58

3 Answers3

7

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.

dknaack
  • 60,192
  • 27
  • 155
  • 202
6

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.

Community
  • 1
  • 1
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • 1
    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. – Alok Swain Jul 08 '14 at 10:00
  • @AlokSwain , Yes but Its good to go by conventions .Why do u make SITE_NAME in caps ,because its global.same is here – Pratik Joshi Jul 08 '14 at 10:01
  • @PratikJoshi Why to implement conventions which have no functionality? – Teemu Jul 08 '14 at 10:02
  • thank you ^^, but I need some time to thinking and reading about an is a private method or mark ... – Anees Hikmat Abu Hmiad Jul 08 '14 at 10:07
  • @AnisHikmatAbu-hmiad , yes you are correct,so please explain "why downvote ? I already told the same in answer " – Pratik Joshi Jul 08 '14 at 10:07
  • "Avoid conventions that demonstrate a lack of competence." [Douglas Crockford](http://javascript.crockford.com/code.html). Btw. I removed my down vote after your last edit. – Teemu Jul 08 '14 at 10:15
1

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.

Alok Swain
  • 6,409
  • 5
  • 36
  • 57