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

Nicktar
  • 5,548
  • 1
  • 28
  • 43
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

6 Answers6

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/

Pavel Gruba
  • 1,999
  • 1
  • 12
  • 8
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

Stefan
  • 14,826
  • 17
  • 80
  • 143
  • What do you expect `self` to be? The global scope, `undefined`? – Bergi Jun 21 '13 at 11:42
  • the funxtion you are currently in – Stefan Jun 21 '13 at 11:43
  • if you are inside the global object space, it is the global object space, if you are in a function, the function – Stefan Jun 21 '13 at 11:43
  • `this` will hardly ever refer to a function, no. Read the [introduction to the `this` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) please. – Bergi Jun 21 '13 at 11:44
  • 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 – dualed Jun 21 '13 at 11:54
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();
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
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...

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.

Zaheer
  • 374
  • 4
  • 20
-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

Community
  • 1
  • 1
Venkat.R
  • 7,420
  • 5
  • 42
  • 63