2

If I create a Class with two method like following:

var Car = function() {}
Car.prototype = {
    run: function() {
        alert('run');
    },
    stop: function() {
        alert('stop');
    }
}

And when the DOM is ready will call test() method,
and this test() method will create a instance of Car and call the method of Car

$(document).ready(function() {
    test("run");
});
function test(method) {
    var instance = new Car();
    // call instance.run()
}

I know I have to use apply() method,
but I have tried lots of times and failed
So, how can I use apply() method in Object?

3
  • perhaps I don't understand the question, but what's wrong on calling instance.run() directly ? Commented Oct 3, 2012 at 10:12
  • Am I missing something here, or you can just do instance.run()? Commented Oct 3, 2012 at 10:13
  • Because I want to call different method by using test(method), sometimes I can call test("run"), sometime test("stop") Commented Oct 3, 2012 at 12:23

5 Answers 5

2

An object in javascript is an associative array, so you can use the [] operator to address by string any member of the object, functions as well.

function test(method) {
    var instance = new Car();
    instance[method]();
}

this will allow to call a function by name over the object Car.

See it working on JsFiddle.

The context inside the called method will properly point to the newly created Car instance without any additional effort, that I guess is what you want. Using apply or call is generally used when you need to change the default 'this' value to something else.

Sign up to request clarification or add additional context in comments.

1 Comment

Try not to use "this" and "new" in your JS code. The best way to master it.
1

I know I have to use apply() method,

You don't. Uncomment the instance.run() code, call the test() method and it works.

demo: http://jsfiddle.net/JkAnb/

how can I use apply() method in Object

It depends on what you want to achieve. There is documentation at MDN.

Comments

1

perhaps you want to call

instance.method()

call dynamically i.e. based on value passed run/stop. You can try this -

instance[method].call(instance)

or

instance[method].apply(instance)

where instance is passed as scope param which is scope of the function to be called (value of "this" inside function).

Comments

0

Why do You need classes in an (object based) functional language? Your intent is bit unclear. What is the implied type of the argument 'method' in Your test function? I assume it is a method name. In any case code bellow might help.

/* prefer this class pattern */
var Car = (function () {
    "use strict";
    Car.prototype = {
        name : undefined ,
        run: function () {
            return name_ + "::  run";
        },
        stop: function () {
            return name_ + "::  stop";
        }
    };
    /* constructor */
    function Car(name) {
        name_ = name;
    }
    return Car;
})();
function printer(msg_) {
   document.body.innerHTML += msg_ + "<br/>";
}
function test(car_, method_name_1, method_name_2) {
       printer(car_[method_name_1]());
       printer(car_[method_name_2]());
}
test( new Car("BMW"), "run", "stop");
test( new Car("Lada"), "run", "stop")

Comments

-2

in addition, you could also use the eval function:

var instance = new Car();
eval('instance.'+method+'();');

though Felice Pollano's way is far neater imo.

http://jsfiddle.net/VZdXb/

1 Comment

Using eval for this case is not necessary, and thus presents a security risk unnecessarily.

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.