1

I just write a test html file to learn about object in javascript. The code is as follows

in script tag

<script type="text/javascript">

    var obj = new ParentFn();
    var obj2 = new AnotherParentFn();
    var temp;
    function initer()
    {
        temp = obj.Adding();
        obj2.caller();
    }
    function ParentFn()
    {
        this.a = 10;
        this.b = 20;
    }
    function AnotherParentFn()
    {
        this.a = 30;
        this.b = 50;
    }
    AnotherParentFn.prototype.caller = function()
    {
        var self = this;
        temp();
    }
    ParentFn.prototype.Adding = function()
    {
        var self = this;
        document.getElementById("id_div1").innerHTML = " Method Called and Result of a+b is " + (self.a + self.b);          
    }

</script>

In body i use

<button onclick="initer()"> Click here to test </button>
<div id="id_div1"></div>

Problem is when AnotherParentFn.prototype.caller is called from initer() function temp variable is still undefined. What is wrong with the code??

My task is to assign the function ParentFn.prototype.Adding in a global variable and call the global variable from AnotherParentFn.prototype.caller function. How to achieve it?

2
  • Your temp variable is already global now Commented Apr 2, 2013 at 5:59
  • Would be great if you could accept any of the answers. Commented Apr 6, 2013 at 4:22

4 Answers 4

1

You don't need to save it as a global variable. It's already saved in ParentFn.prototype. All you need to do is invoke it with .call and pass in your desired receiver. You can implement AnotherParentFn.prototype.caller like this:

AnotherParentFn.prototype.caller = function()
{
    ParentFn.prototype.Adding.call(this);
}

This way you can get rid of temp completely. You also don't need to assign this to a local var self everywhere.

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

Comments

0

Parentheses are used to execute a function. When you assign the value to temp, you are calling the function and assigning the result (undefined) to temp. To store a reference to the function in temp, omit the parentheses.

temp = obj.Adding;

Comments

0

By writing temp = obj.Adding(); it stores the return value. not function pointer in temp. Use this

function initer()
{
    temp = obj.Adding;
    obj2.caller();
}

Comments

0

First of all, the reference to obj.Adding is not assigned properly; it should be this (without parentheses):

function initer()
{
    temp = obj.Adding;
    obj2.caller();
}

Then, inside AnotherParentFn.prototype.caller itself, you must pass the current object as this explicitly during the invocation by using .call():

AnotherParentFn.prototype.caller = function()
{
    temp.call(this);
}

Comments

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.