Javascript属性检查器功能(使用闭包) [英] Javascript property inspector function (using closure)

查看:62
本文介绍了Javascript属性检查器功能(使用闭包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个可用于检查对象属性的函数。

I am trying to write a function that can be used to inspect the properties of an object.

例如,对于给定的对象:

For example, for the given object:

var obj = {
    s : 'a',
    n : 1,
    d : new Date(),
    f : function(){},
    a : [],
    o : {
        a : {
            b: 'b'
        }
    }
};

我可以使用检查器功能访问属性:

I could use my inspector function to access to properties:

var ins = inspector(obj);
ins('s') // -> a
ins('n') // -> 1
ins('d') // -> 2018-12-06T11:51:26.244Z
ins('f') // -> [Function]
ins('a') // -> []
ins('o') // -> { a: { b: 'b' } }
ins('o.a') // -> { b: 'b' }
ins('o.a.b') // -> b
ins('o.p') // -> undefined
ins('o.p.p') // -> undefined
ins('p') // -> undefined
ins('') // -> undefined

我的第一种方法:

function inspector(obj){
    return function fun (prop){
        console.log(obj[prop])
    }
}

但是,在某些情况下它不能正常工作:

However, it does not works properly for some situations:

ins('o.a') // -> { b: 'b' }
ins('o.a.b') // -> b
ins('o.p') // -> undefined
ins('o.p.p') // -> undefined

如何重写函数以适应这些情况?

推荐答案

您可以使用 reduce 来获取值。首先,您需要按分割路径。

You can use reduce to get the value. First, you need to split the path by .:

function inspector(obj){
    return function fun (prop){
        return prop.split(".").reduce(function(o, p) {  // for each part p in prop.split(".")
            return o && o[p];                           // if o is an object truthy return o[p]
        }, obj);                                        // pass obj as the initial o
    }
}

测试 o& 是非常基本的。如果仅当 o 是对象时,才想对 o 下标,则将该行替换为:

The test o && is very basic. If you want to subscript o if and only if o is an object then replace that line with:

return (o && typeof o === "object")? o[p]: undefined;

如果<<则返回 o [p] code> o 是一个对象,否则为未定义

Which returns o[p] if o is an object, otherwise undefined.

示例:

function inspector(obj){
    return function fun (prop){
        return prop.split(".").reduce(function(o, p) {
            return (o && typeof o === "object")? o[p]: undefined;
        }, obj);
    }
}

var obj = {
    s : 'a',
    n : 1,
    d : new Date(),
    f : function(){},
    a : [],
    o : {
        a : {
            b: 'b'
        }
    }
};

var ins = inspector(obj);

console.log('s:', ins('s'));          // -> a
console.log('n:', ins('n'));          // -> 1
console.log('d:', ins('d'));          // -> 2018-12-06T11:51:26.244Z
console.log('f:', ins('f'));          // -> [Function]
console.log('a:', ins('a'));          // -> []
console.log('o:', ins('o'));          // -> { a: { b: 'b' } }
console.log('o.a:', ins('o.a'));      // -> { b: 'b' }
console.log('o.a.b:', ins('o.a.b'));  // -> b
console.log('o.p:', ins('o.p'));      // -> undefined
console.log('o.p.p:', ins('o.p.p'));  // -> undefined
console.log('p:', ins('p'));          // -> undefined
console.log(':', ins(''));            // -> undefined

这篇关于Javascript属性检查器功能(使用闭包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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