0

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.

5
  • I know that methods are functions on objects, or inside classes, and that functions are independent. Commented Oct 6, 2013 at 20:23
  • Can you show us what you would want to pass as an argument and what the desired result would be? Commented Oct 6, 2013 at 20:24
  • What you expect to see as result. Your code is correct and acts as it should. Commented Oct 6, 2013 at 20:29
  • @Bergi Exatcly what Artyom Neustroev posted as accepted answer. Pass property name as argument in method, than check if exist (hasOwnProperty - I wanted to iterrate trough all properties, silly me) and then return/log value of that property. Commented Oct 6, 2013 at 20:37
  • @Krasimir See comments in code, I'm getting what I want as results. P.S. Do I use too much comments? Commented Oct 6, 2013 at 20:38

1 Answer 1

1

Solution

You should use [] notation to access the object's property by its name.

Create a method inside your constructor:

function Foo(prop1val, prop2val) {
   this.prop1 = prop1val;
   this.prop2 = prop2val;
   this.PrintProperty = function (name) {
      if (this.hasOwnProperty(name)) {
         console.log(this[name]);
      }
   }
}

Now call it:

var bar = new Foo("prop1val", "prop2val");
bar.PrintProperty("prop1");

Links

  1. Property Accessors - JavaScript | MDN
  2. hasOwnProperty - JavaScript | MDN
Sign up to request clarification or add additional context in comments.

1 Comment

@Artyom Neustroev That was exactly what I've been searching for. Check comments above. Now I know difference in calling method or functions outside "class" in JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.