Javascript方括号表示法多个动态属性 [英] Javascript Square Bracket Notation Multiple Dynamic Properties

查看:201
本文介绍了Javascript方括号表示法多个动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能有点不寻常,我以前从来不需要使用方括号表示法,而且绞尽脑汁,我想不出一种产生期望结果的方法.

This may sound a bit unusual, I've never needed to use square bracket notation in this way before, and racking my brains I can't think of a way to produce the desired outcome.

我正在实现一个回调包装器,以在将方法作为回调传递时保持对this的引用

I'm implementing a callback wrapper to maintain the reference of this when passing methods as callbacks

例如

foo.prototype.wrap = function(name){
    var wrapper,
        self = this;

    wrapper = function(){
        self[name](arguments);
    };

    return wrapper;
};

// usage

foo.wrap('bar');

// executes foo.bar maintaining 'this' as a reference to foo 

我遇到的问题是foo有一些嵌套方法

The issue I'm having is that foo has some nested methods

例如

foo.bar.close();

我正在尝试找到一种使wrap方法支持嵌套方法的方法

I'm trying to figure out a way to make the wrap method support nested methods

例如

foo.wrap('bar.close')

// or

foo.wrap('bar','close');

因此,foo.wrap函数将需要动态添加与传入的长度或参数相对应的方括号.

So the foo.wrap function would need to dynamically add the square brackets corresponding to the length or the arguments passed in.

例如

self[x][y][z](arguments);

我想不出办法.有任何想法吗 ?

I can't think of a way to do this. Any ideas ?

我有一个偷偷的怀疑,但这是不可能的.

I have a sneaking suspicion this isn't possible though.

推荐答案

在那些日子里,我一定会忘记一切:)

I must be having one of those days where you forget everything :)

虽然@NilColor的答案是正确的,但我确实知道我只是没有考虑戴上正确的帽子.

While @NilColor's answer is correct, and I did know it I just wasn't thinking with the correct hat on.

无论如何,我还是决定我喜欢一个包装器,当您将包装器附加到对象上时,该包装器需要较少的特殊性.而且有点冗长.

Anyway I decided that I still like the idea of having a wrapper that requires a bit less specificity when you attach it to your objects. And is a bit less verbose.

所以我将其与我最初的思路一起写了,您可能会喜欢.

So I wrote it along with my original line of thinking, you might like it.

myObj.wrap = function(path, context){ 
    var wrapper,
        method = ( typeof path != 'string' && context )? path : this,
        context =  (typeof path === 'object' && context === undefined)? 
            path : (context || this);

    if (typeof path === 'string'){
        path = path.split('.');

        for ( var i = 0; i < path.length; i++ ){
            method = method[path[i]];
            if ( context === true  )
                context = ( i === path.length - 2 )? method : context; 
        };
    };

    wrapper = function(){
        method.apply(context, arguments);
    };

    return wrapper;
}

用法:

将任意数量的嵌套方法绑定到myObj

Bind any number of nested methods to myObj

    myObj.wrap('foo') //binds myObj.foo to myObj

// or

    myObj.wrap('foo.bar') //binds myObj.foo.bar to myObj

//or if myObj is a function

    myFunc.wrap() // binds myFunc to myFunc

将myObj的方法绑定到另一个对象

Bind a method of myObj to another object

    myObj.wrap('foo.bar', someObj) //binds myObj.foo.bar to someObj

//or if myObj is a function

    myFunc.wrap(someObj) //Binds myFunc to someObj

将嵌套方法绑定到其父方法

Bind a nested method to it's parent

    myObj.wrap('foo.bar', true) // binds myObj.foo.bar to myObj.foo

用作助手

    myObj.wrap(someFunc, someObj) //binds someFunc to someObj

如果您不是在方法绑定的上下文中寻找原始问题的答案.

myObj.getProps = function(path, context){
var context = context || this;
    path = path.split('.');


for ( var i = 0; i < path.length; i++ ){
            context = context[path[i]];
    };

    return context;
};

用法:

附加到对象或作为独立函数

attach to an object or as a standalone function

获取属性

myObj.getProps('foo.bar') // returns mayObj.foo.bar

为其提供上下文对象

myObj.getProps('user.name', myAccounts) // returns myAccounts.user.name

将其用作独立功能替换

myObj.getProps = function(path,context){....}

使用

function getProps(path,context){....}

注意

如果将其用作独立功能,则需要记住它将从this的范围开始查找.因此,如果是在全局范围内定义的,则需要提供完整的路径.

If using it as a standalone function you will need to remember that it will start looking from the scope of this. So if it's defined in the global scope you need to provide full paths.

例如

getProps('myObj.foo.bar')

您仍然可以使用上下文选择器更改参考对象.

You can still use the context selector to change the reference object.

这篇关于Javascript方括号表示法多个动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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