How to use "call" method with parameters

by Jagadish Pulakhandam on 9/10/2012 8:57:14 AM
Rated 0 from 0 votes
Brief: Demonstrates on calling a method from another instance using "call" method with parameters
Posted to: Advanced JavaScript Programming/Development tutorials/videos- Object Oriented Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Source Code/Video demonstrates the following:
  • Creating two instances from two different classes
  • Calling a method in one instance on behalf of another instance
  • Passing parameters to the being "called" (or executed) to work with data from another instance
  • using "call" method in JavaScript to call methods from other instances (by using data from current instance)
Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script type="text/javascript">
07.        //DEMO: using 'Call' method with parameters
08.                 
09.        //define a function
10.        var Emp = function (vId, vName) {
11.            this.Id = vId;
12.            this.Name = vName;
13.            //method
14.            this.ShowDetails = function (IdCaption, NameCaption) {
15.                alert(IdCaption + this.Id.toString() + ", " + NameCaption + this.Name);
16.            }
17.        }
18. 
19.        //define another function,
20.        //which contains a couple of properties similar to previous function
21.        var Customer = function (vId, vName) {
22.            this.Id = vId;
23.            this.Name = vName;
24.        }
25. 
26.        //create instances
27.        var oCustomer = new Customer(1001, "Jag");
28.        var oEmp = new Emp(2001, "Chat");
29. 
30.        //call ShowDetails with parameters on behalf of oCustomer (similar to oCustomer.ShowDetails)
31.        oEmp.ShowDetails.call(oCustomer, "Id : ", "Name : ");
32. 
33.    </script>
34.</head>
35.<body>
36. 
37.</body>
38.</html>


Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following