alankis

{Javascript} Difference in calling method on object or pass

Oct 6th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // create object constructor
  2. function Foo(prop1, prop2) {
  3.     this.prop1 = prop1;
  4.     this.prop2 = prop2;
  5.     // create object method PrintObjectPropertyM
  6.     // and retreive property value
  7.     // * there is M at the end of property to distinguish
  8.     // method/function name
  9.    
  10.     // how can I add placeholder/argument in method and then
  11.     // call method on object with provided argument (here prop1 or prop2)
  12.     this.printObjectPropertyM = function() {
  13.         console.log(this.prop1);
  14.     };
  15. }
  16.  
  17. // instantiate new object Bar of Foo type
  18. var Bar = new Foo("prop1val", "prop2val");
  19.  
  20. // create function which print object property and take object as an argument
  21. var printObjectProperty = function(object) {
  22.     console.log(object.prop1);
  23. };
  24.  
  25. // call printObjectProperty with Bar object as an argument
  26. printObjectProperty(Bar); // logs prop1var in console
  27.  
  28. // call Bar method printObjectPropertyM
  29. Bar.printObjectPropertyM(); // logs prop1val in console
Advertisement
Add Comment
Please, Sign In to add comment