We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 12/02/07


Tagged

method function call


Versions (?)


Function call() Method


Published in: JavaScript 


This method allows you to call a function as a method of another object. The first argument the call method expects is the object it is to operate on. Any others are part of the function. Note how the this keyword now refers to the comp object, so a result property is created inside comp.

  1. function containerScope(){
  2. var comp = { //create a new object
  3. a: 22, //set some properties
  4. b: 33
  5. };
  6. function square(x){ //create a new function
  7. this.result = x*x;//this refers to the object when used in call()
  8. }
  9. square.call(comp, 4); //call the square function as a method of comp object
  10. console.dir(comp); //note how a comp.result property has now been created
  11. }

Report this snippet 

You need to login to post a comment.