What is difference in calling method on object or passing object as function argument, and accordingly to that how do I pass argument in method call? Simply putting placeholder as argument didn't produced desired effect. Do I have to iterate trough all property names(not values) and then compare given argument (as property name) to real property name in object?
Here is heavily commented piece of code, because I'm curious what exactly is happening.
// create object constructor
function Foo(prop1, prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
// create object method PrintObjectPropertyM
// and retreive property value
// * there is M at the end of property to distinguish
// method/function name
// how can I add placeholder/argument in method and then
// call method on object with provided argument (here prop1 or prop2)
this.printObjectPropertyM = function() {
console.log(this.prop1);
};
}
// instantiate new object Bar of Foo type
var Bar = new Foo("prop1val", "prop2val");
// create function which print object property and take object as an argument
var printObjectProperty = function(object) {
console.log(object.prop1);
};
// call printObjectProperty with Bar object as an argument
printObjectProperty(Bar); // logs prop1var in console
// call Bar method printObjectPropertyM
Bar.printObjectPropertyM(); // logs prop1val in console
Please be kind and correct me if is something wrong in my comments, code or pseudocode.