2

From my object iteration, i am trying to call the function and sending data as parameter, on my try, i am getting error..

what is the proper approach to call the function using the object keys..?

my try:

var x = function(msg){
    console.log(msg);
}
var y = function(msg){
    console.log(msg);
}

    var obj = {x:"i am x", y:"i am y"}

    var all = function(){
        $.each(obj,function(key,value){
           [key](value);
        })
    }

    all();

any one figure ou the correct approach pls.. here is the jsfiddle link

0

6 Answers 6

8

Try this way

var funcs = {
    x: function(msg){
        console.log(msg);
    },
    y: function(msg){
        console.log(msg);
    }
};

var obj = {x:"i am x", y:"i am y"}

var all = function(){
    $.each(obj,function(key,value){
       funcs[key](value);
    })
}

all();

Here is your fiddle http://jsfiddle.net/9XqeJ/1/

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

3 Comments

yes, Understand. By we can't directly call a regular function? if so why..?
Because you cannot access simple variables by name. Bracket notation works only for properties of objects.
you can declare your functions 'x' and 'y' as properties of global objects (declare it without 'var'), but it's bad
1

This won't do what you want..

var x = function(msg){
    console.log(msg);
}
var y = function(msg){
    console.log(msg);
}

defines two indpendetn functions.

var obj = {x:"i am x", y:"i am y"}

Defines an object with the keys 'x' and 'y'. They are completly independent of the funcitons you defined.

You can do this:

var self = this;
this.x = function(msg){
    console.log(msg);
}
this.y = function(msg){
    console.log(msg);
}

var obj = {x:"i am x", y:"i am y"}

var all = function(){
    $.each(obj,function(key,value){
        self[key](value);
    })
}

all();

To call a funciton named with the key. Fiddle

5 Comments

What do you expect self to be? The global scope, undefined?
the funxtion you are currently in
if you are inside the global object space, it is the global object space, if you are in a function, the function
this will hardly ever refer to a function, no. Read the introduction to the this keyword please.
this will refer to the global environment aka window in browsers, unless you put the code in a function and somehow get this from an object or dom element
0

If your variables scope is global, you could use that but not the best way:

http://jsfiddle.net/PWtEG/

 x = function (msg) {
     console.log(msg);
 }
 y = function (msg) {
     console.log(msg);
 }

 var obj = {
     x: "i am x",
     y: "i am y"
 }

 var all = function () {
     $.each(obj, function (key, value) {
         window[key](value);
     })
 }

 all();

Comments

0
var objF = {
    set setO(v) {
        for (var k in v) {
            objF[k](v[k]) // or this[k](v[k]) or add to internal objF object/var/array
        }
    },
    x: function(msg) {
        console.log(msg)
    },
    y: function(msg) {
        console.log(msg)
    }
}
objF.setO = {x:"i am x", y:"i am y"}

Could also add the object with set/get and then post that way if you wanted to use them later...

Comments

0

You can use jQuery function jQuery.globalEval

var x = function(msg){
    console.log(msg);
}
var y = function(msg){
    console.log(msg);
}

var obj = {x:"i am x", y:"i am y"}

var all = function(){
  $.each(obj,function(key,value){
    jQuery.globalEval(`${key}("${value}")`)
  })
}

all();

"${value}" Expression contains the quotes to form a string. For other values, it should be without quotes, otherwise function would get everything as string. You need to add a check on value type i.e. typeof value and decide to add quotes in jQuery.globalEval call or not.

Comments

-1

Here the code to implement

var key = "foo";
obj[key](1, 2, 3);
obj[key].call(obj, 1, 2, 3);
obj[key].apply(obj, [1, 2, 3]);

function foo() { console.log(arguments); }

// 1. directly
foo(1, 2, 3);

// 2. trough Function.call()
foo.call(this, 1, 2, 3);

// 3. trough Function.apply()
var args = [1, 2, 3];
foo.apply(this, args);

SO Answer: javascript equivalent of php call_user_func

2 Comments

The above solution from native javascript and its working in all the browser. Proof is phpjs.org/functions/call_user_func_array
Yes. Only it doesn't answer the question.

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.