Solved

Javascript with callback

Posted on 2011-03-01
2
274 Views
Last Modified: 2012-05-11

Hi

I have a simple piece of example code, in which I am passing a function (for call back) into a class.
When this function is called back, I wish to have a parameter passed back but this is not working.

Thanks for you help
Phil
<?php
  
?>

<!DOCTYPE html>
<head>

<script language='javascript' type='text/javascript'>
window.onload=init;

function init() {
    var y;
    var myClassObj = new MyClass(5, calledBack); 
}

function calledBack(x) {
    alert("Got called back with "+x);
}

    
function MyClass(val, f)
{
    var data = val;
    
    function someFunc(x) 
    {
       alert("someFunc "+x);
       f.call(x);
    }
    
    someFunc(data);
}
</script>


</head>
<body>

<div style="" >

</div>

</body>
</html>

Open in new window

0
Comment
Question by:BeerFizz
2 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
ID: 35012289
change your code line #28 from
f.call(x);
to
f(x);
0
 

Author Closing Comment

by:BeerFizz
ID: 35012321
Perfect!  Thank you.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now