繁体   English   中英

如何在本机javascript中的动态对象函数中获取函数名

[英]How to get function name within dynamic object function in native javascript

我正在创建一个js库,如下所述直接处理来自集合对象的项目操作。 我想允许为每个项目添加动态用户定义的对象函数,并使这些函数直接通过带有索引的集合对象可用(请参见示例实现)。

var item = function (obj) {
    this.foo = function () {
        alert("foo" + this.name);
    }
    for (var i in obj) {
        this[i] = obj[i]; //dynamic fields (overridable)
    }
}
var collection = function (list) {
    var self=this;
    this.items = [];
    if (list && list.length > 0) {
        for (var i in list) {
            var t = new item(list[i]); // create new item            
            for (var j in t) {
                if (typeof (t[j]) === "function") {
                    this[j]=function(index,arg1,arg2,arg3){                        
                        self.items[index][j](arg1,arg2,arg3); //not working (unreliable)
                        console.log(j) //undefined
                        // problem is here j cannot be visible from function handler.
                        //I need to call dynamic j function within handler
                    }
                }
                this.items.push(t); //push each item into collection
            }
        }
    }
}
var obj=new collection([
    {
        id:1,name:"apple",
        bar:function(arg){
            alert(arg+" "+this.name);
        }
    }
    ]);
    obj.foo(0); //equivalent to obj.items[0].foo();
    obj.bar(0,"parameter"); //equivalent to obj.items[0].bar("parameter");

如上述代码段中的注释所述,我无法从集合函数动态调用项目的函数。 我怎样才能达到这个目的。 请帮我。

您无法从处理程序的范围中获取相关的字段名称。 您需要做的是使用字段名称创建新的函数对象,即,函数名称=字段名称。 为此,您必须做一些技巧来指定项目对集合对象的动态功能。 在处理程序范围内,可以通过arguments.callee强制转换函数名称
更改此代码段

var t = new item(list[i]); // create new item            
        for (var j in t) {
            if (typeof (t[j]) === "function") {
                this[j]=function(index,arg1,arg2,arg3){                        
                    self.items[index][j](arg1,arg2,arg3); //not working (unreliable)
                    console.log(j) //undefined
                    // problem is here j cannot be visible from function handler.
                    //I need to call dynamic j function within handler
                }
            }
            this.items.push(t); //push each item into collection
        }

对此:

this.items.push(new item(list[i]));    
        var t=this.items[this.items.length-1];
        for (var j in t) {
           if (typeof (t[j]) === "function") {
                this[j]=new Function( "return function " + j + "(idx,arg1,arg2,arg3){console.log(this.items[idx]);var fn = arguments.callee.toString().substr('function '.length);fn = fn.substr(0, fn.indexOf('(')); if(this.items[idx][fn])this.items[idx][fn](arg1,arg2,arg3);}")();                    
            }
          //  this.items.push(t); //push each item into collection
        }                    
                }

jsfiddle示例: http : //jsfiddle.net/kyawlay/7528t7z8/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM