1

I see a js function that name with a underline.So I try to write like this way,but I can not call the function.Why can a js function named only by _? Can any one explain this syntax?

function Test(){
    var a=1;
    function _(abc){
        console.log(abc);
    } 
}

Then try to call the function but nothing happen

(new Test()._(dsdf));
1
  • usually private function starts with _ Commented Aug 18, 2016 at 6:40

2 Answers 2

1

Your function is not visible out of the Test.In your example you create an object of Test.If your want to call an function on the object it must be a member of the object.So you need to use 'this' inside the function.

function Test() {
   var a=1;

   this._ = function(abc) {
      console.log(abc);
   } 
}

new Test()._('aa');

Sign up to request clarification or add additional context in comments.

6 Comments

But I really see a _() function defined in a Test() function.That are not using this key word.
@flower in the browser?
How can I talk with you ?I will send you a picture.
You can create a chat
How to create a chat,please?
|
1

Initially you have to make a minor change (look at the end of this answer to read about the necessity of this change) in the way you call the Test constructor. Then you have to make a major change in the way you define Test Below are the changes:

(new Test())._('dsdf');

Initially you create an object based on your constructor by using the new operator. Later on, on this object you call the _ method passing to it a string 'dsdf'.

function Test(){
    this._ = function( abc ){
    console.log( abc );
    } 
}

(new Test())._('dsdf');

new Test()._('dsdf');

Note in the above snippet the use of this. When you call the Test using the new operator, in the object that would be created there would have been defined a method called _, which expects one parameter and outputs that parameter to the console.

Why you have to change the definition of Test?

This is needed because in the initial definition of Test, the function called _ is not visible to others. It is defined in the scope of Test and it can be called only by code inside the body of Test.

The minor change can be omitted, as you can fairly easy note by running the snippet. However, in my personal opinion the first code is more readable. Furthermore, it would be even more readable, if you had separated the creation of your object and the call of the method, like below:

var test = new Test();
test._('dsdf');

2 Comments

But I really see a _() function defined in a Test() function.That are not using this key word.Is _() function like a constructor function?
Yeap, it is defined but not in the context of this. When you write new Test the _ would not be defined on the object you create.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.