使用 javascript eval 函数传递参数时出现问题...我做错了什么?

发布于 2024-09-15 02:34:27 字数 1032 浏览 5 评论 0原文

像往常一样,我在测试了几个小时后来到这里,没有任何线索我做错了什么;-) 我不是 javascript 专家,所以我想这会导致一些翻白眼;-) 对此感到抱歉。我现在正尝试以更易读的方式编写代码。分离类中的函数等...我想要实现的是这样的:


modules.create('streamModule','leftColumn')
这将调用
streamModule.create()


现在我无法开始工作:
调用 modules.create('streamModule','leftColumn') 总是会导致
参考错误:leftColumn未定义???
调用中的id:eval(type +".create("+id+","+target")");总是正确传递,但目标参数不是???我做错了什么?

任何帮助将非常非常感激!谢谢你!

var modules = {
    _map    : [],

    create: function(type,target) {

    if(type == undefined) return false;
    if(target == undefined) return false;

    //Store id in map
    this._map.push(id = createID());

    //Call create function of module
    eval(type +".create("+id+","+target")");
    }
}

[...]

var streamModule = {

create: function() {
    $.log(target)
   }
}

[...]

modules.create('streamModule','leftColumn') 

as usual i come here after testing around for hours and not having any clue what i am doing wrong ;-) I am not an javascript expert so i suppose this will cause some rolling eyes ;-) Sorry about that. I am now at a point where i tried to code somehting in a more readable way. Seperating functions in classes etc... What i want to achieve is something like this:


modules.create('streamModule','leftColumn')
which will call
streamModule.create()


NOW THE THING I DONT GET TO WORK:
Calling modules.create('streamModule','leftColumn') always results in
Reference Error: leftColumn is not defined???
The id in the call: eval(type +".create("+id+","+target")"); is always passed correctly but the target parameter is not??? What am i doing wrong?

Any help would be very very appreciated!!!!! Thank you!

var modules = {
    _map    : [],

    create: function(type,target) {

    if(type == undefined) return false;
    if(target == undefined) return false;

    //Store id in map
    this._map.push(id = createID());

    //Call create function of module
    eval(type +".create("+id+","+target")");
    }
}

[...]

var streamModule = {

create: function() {
    $.log(target)
   }
}

[...]

modules.create('streamModule','leftColumn') 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-09-22 02:34:27

为什么你需要eval

var modules = {
    _map    : [],

    create: function(type,target) {

    if(type == undefined) return false;
    if(target == undefined) return false;

    //Store id in map
    this._map.push(id = createID());

    //Call create function of module
    type.create(id, target);
}

[...]

var streamModule = {

   create: function(id, target) {
    $.log(target)
   }
}

[...]

modules.create(streamModule, leftColumn);

Why do you need eval at all?

var modules = {
    _map    : [],

    create: function(type,target) {

    if(type == undefined) return false;
    if(target == undefined) return false;

    //Store id in map
    this._map.push(id = createID());

    //Call create function of module
    type.create(id, target);
}

[...]

var streamModule = {

   create: function(id, target) {
    $.log(target)
   }
}

[...]

modules.create(streamModule, leftColumn);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文