最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Return values with call() or apply() in JavaScript - Stack Overflow

programmeradmin4浏览0评论

Is there a way to get the function's return value when it's called from call() or apply()?

My scenario:

I have a function that works fine with apply() function. On object contructor:

var someObject = new SomeObject({NameFunction: "MakeNames"});

On a loop in the object's method:

var name = "";
for (var i = 0; i < data.length; i++) {
    name = window[this.NameFunction].apply(null, [data[i]]);
}

And the MakeNames function:

function MakeNames(data)
{
    return data.FirstName + " " + data.LastName;
}

That var name stay empty cause apply doesn't return any value from the function. Note that I'm sure the function is called and the argument is passed succesfully. Please don't tell me to use name = MakeNames(data[i]); directly, because it is obvious that I don't want this solution.

It doesn't matter if it is apply, call or whatever JavaScript has to make this work, if works.

How can I achieve this?

Is there a way to get the function's return value when it's called from call() or apply()?

My scenario:

I have a function that works fine with apply() function. On object contructor:

var someObject = new SomeObject({NameFunction: "MakeNames"});

On a loop in the object's method:

var name = "";
for (var i = 0; i < data.length; i++) {
    name = window[this.NameFunction].apply(null, [data[i]]);
}

And the MakeNames function:

function MakeNames(data)
{
    return data.FirstName + " " + data.LastName;
}

That var name stay empty cause apply doesn't return any value from the function. Note that I'm sure the function is called and the argument is passed succesfully. Please don't tell me to use name = MakeNames(data[i]); directly, because it is obvious that I don't want this solution.

It doesn't matter if it is apply, call or whatever JavaScript has to make this work, if works.

How can I achieve this?

Share Improve this question edited May 24, 2015 at 1:19 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 22, 2013 at 12:59 DontVoteMeDownDontVoteMeDown 21.5k10 gold badges71 silver badges113 bronze badges 7
  • return values work ok for me. running this in the browser console var x = "huh".toString.apply("test"); set x to "test" for me. – Ben McCormick Commented May 22, 2013 at 13:02
  • 1 apply and call will return values. Something else must be wrong. can you supply more of the code that exposed the function you're trying to call? Also what is toString doing? your function name is already a string in your example. – Tim Commented May 22, 2013 at 13:04
  • @ben336 Sorry dude, can't understand your point. Can you post a more accurate answer? – DontVoteMeDown Commented May 22, 2013 at 13:08
  • @Tim disconsider that toString(). I removed it and the result stays the same. – DontVoteMeDown Commented May 22, 2013 at 13:09
  • @DontVoteMeDown My answer is very accurate :) As is the other ment and RobG's answer. The point is that call and apply don't prevent you from returning a value. Your issue is elsewhere. – Ben McCormick Commented May 22, 2013 at 13:10
 |  Show 2 more ments

2 Answers 2

Reset to default 13

The result of:

window[this.NameFunction.toString()].apply(null, [data[i]])

is exactly the same as:

MakeNames(data[i])

since the only difference is the value of this in the MakeNames function, and since this isn't used, it can't make any difference. The use of apply doesn't change whether the function returns a value or not, or what the value is.

Whatever issue you have, it isn't in the (bits of) posted code.

I don't see in your code where data is set, or where data[i] would be an object that has the properties FirstName and LastName. Try a fail-over:

function MakeNames(data) {
    return (data.hasOwnProperty('FirstName')) ? data.FirstName + " " + data.LastName : 'John Doe';
}
发布评论

评论列表(0)

  1. 暂无评论