javascript将dotnotation字符串转换为对象 [英] javascript convert dotnotation string into objects

查看:37
本文介绍了javascript将dotnotation字符串转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从客户端传递来的字符串,例如"namespace.fun1.fun2.fun3" .告诉服务器使用哪个功能.

I have a string like this "namespace.fun1.fun2.fun3" passed from the client. It's telling the server which function to use.

如何安全运行该功能?

现在我正在做:

var runthis = string.split('.')
namespace[runthis[1]][runthis[2]][runthis[3]]

如何安全处理任意深度的嵌套?

How do I handle arbitrary depth of nesting safely?

推荐答案

我编写的一个小函数.我在大多数应用程序中都使用了它:

A little function I wrote. I use it in most of my applications:

Object.lookup = (function _lookup() {
    var cache = { };

    return function _lookupClosure( lookup, failGracefully ) {
        var check   = null,
            chain   = [ ],
            lastkey = '';

        if( typeof lookup === 'string' ) {
            if( cache[ lookup ] ) {
                chain = cache[ lookup ].chain;
                check = cache[ lookup ].check;
            }
            else {
                lookup.split( /\./ ).forEach(function _forEach( key, index, arr ) {
                    if( check ) {
                        if( typeof check === 'object' ) {
                            if( key in check ) {
                                chain.push( check = check[ key ] );
                                lastkey = key;
                            }
                            else {
                                if( !failGracefully ) {
                                    throw new TypeError( 'cannot resolve "' + key + '" in ' + lastkey );    
                                }
                            }
                        }
                        else {
                            if( !failGracefully ) {
                                throw new TypeError( '"' + check + '" ' + ' does not seem to be an object' );   
                            }
                        }
                    }
                    else {
                        lastkey = key;
                        chain.push( check = window[ key ] );
                    }
                });

                if( check ) {
                    cache[ lookup ] = {
                        chain: chain,
                        check: check
                    };
                }
            }
        }

        return {
            execute: function _execute() {
                return typeof check === 'function' ? check.apply( chain[chain.length - 2], arguments ) : null;
            },
            get: function _get() {
                return check;
            }
        };
    }
}());


用法:

Object.lookup( 'namespace.fun1.fun2.fun3' ).execute();

第一个参数是要解析的对象/方法/属性.第二个(可选)参数指示 lookup()方法是否应静默失败或在某些属性或对象无法解析的情况下引发异常.默认值为"throw".为避免通话

The first parameter is the object/method/property to resolve. The second (optional) parameter indicates whether or not the lookup() method shall fail silently or throw an exception if some property or object could not get resolved. default is 'throw'. To avoid that call

Object.lookup( 'namespace.fun1.fun2.fun3', true ).execute( 'with', 'paras' );

如果 .fun3 是一个函数,则可以将任何参数传递给 .execute().如果只想接收属性值,也可以调用 .get()而不是 .execute()

If .fun3 is a function, you can pass in any parameters into .execute() instead. if you just want to receive the property value, you can also call .get() instead of .execute()

var val = Object.lookup( 'namespace.fun1.fun2.fun3' ).get();

这篇关于javascript将dotnotation字符串转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆