0

I have a function as below. My Problem is that I need to print function b() inside a function. Here function a() is parent function. and I have done it by taking c= new a() and console.log(b()) . I got the right value as I expected. But inside the parent function I have another function named test() and with same name there is another function outside the parent function. After printing function b() I call the function test() . But it return the value from the parent function. I need to get an alert as needed answer which is in outer function. Is there any way to get my expected result without changing the function name.? Both functions test() are needed.

Expected result: function b() = 2 and function test() = needed answer

Current result : function b() = 2 and function test() = 4

Any help will be appreciated. Thank You.

function a() {
 var val = 1;
 var otherval = 2;
 
        b = function() {
  return val + 1;
        }
 
        test = function() {
           qwerty = otherval + b();
           alert(qwerty);
        } 

   return 'OK';  
 }
 
function test(){
    alert('needed answer');
}
  
c = new a();
console.log(b()); //return 2 
test(); //alert 4 but I needed alert needed answer
Adharsh M
  • 2,773
  • 2
  • 20
  • 33
Arun
  • 1,609
  • 1
  • 15
  • 18

2 Answers2

2

As Rajesh Pointed out, declaring a function without using var will make it a global variable. Use var before test function to get expected value. But this will make it inaccessible outside the function a(). For that, assign the functions to this object and access then as shown below.

function a() {
 var val = 1;
 var otherval = 2;
    this.b = function() {  //prepend this
     return val + 1;
    }
    this.test = function() {  //prepend this
        qwerty = otherval + b();
        alert(qwerty);
    } 
    return 'OK';  
}
 
function test(){
 alert('needed answer');
}
  
c = new a();
console.log(c.b()); //return 2 
test(); //alert 4
Saksham
  • 9,037
  • 7
  • 45
  • 73
  • First thanks for crediting. Now you have fixed 1 problem, but have left `b`. It should throw error(*which was, in your previous edits*). Second, if the only problem is missing `var`, it makes this as a typographical mistake and it **should** be closed and not answered. – Rajesh Jun 07 '17 at 09:59
  • @Rajesh now it should be fine? in order to make function `b` accessible, it should be assigned to the `a`? – Saksham Jun 07 '17 at 10:07
  • @Rajesh I would like to see your detailed comments on this as we all are here to learn – Saksham Jun 07 '17 at 10:10
  • Taking back my vote, still I believe this is a typographical question and should not be answered. Since your fix falls in gray area, not voting. But still its wrong in my understanding. – Rajesh Jun 07 '17 at 10:10
  • Your `this.test` will fail. See this is why I didn't want anyone to answer. This adds lots of blank areas and makes lot of repeated conversations to fix and we are still not clear on OP's intentions. – Rajesh Jun 07 '17 at 10:12
  • @Rajesh I did vote to close but if eventually it doesn't, I thought of highlighting your comment so that whoever lands to this page should get help. I did wait a while for you to post it as an answer – Saksham Jun 07 '17 at 10:13
1

You need slight modification in your code. Do this and it will work as per your requirement -

function a() {
 var val = 1;
 var otherval = 2;
 
    b = function() {
  return val + 1;
 }
 
    this.test = function() {
  qwerty = otherval + b();
   alert(qwerty);
 } 
   return 'OK';  
}
 
function test(){
 alert('needed answer');
}
  
c = new a();
alert(this.b()); //return 2 
test(); //alert 4
Rudraksh Pathak
  • 888
  • 8
  • 20