Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js]对象属性检索 #18

Open
VaJoy opened this issue Jan 19, 2016 · 9 comments
Open

[js]对象属性检索 #18

VaJoy opened this issue Jan 19, 2016 · 9 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented Jan 19, 2016

判断对象是否存在某属性,若有则返回属性值,否则返回undefined

  • 示例:
    var o = {
      a : {
        b : {
          c : 11
        }
      },
      b : 2
    };
    Object.prototype.hash = function(attr){
      //TODO: 完成该方法
    };
    o.hash('a.b.c');  //11
@LittleBearBond
Copy link

Object.prototype.hash = function(attr) {
    return attr && attr.split('.').reduce(function(pre, next) {
        return pre && pre[next];
    }, this);
};

var a = {
    a: {
        b: {
            c: 1
        }
    }
}
a.hash('a.b.c')

@inJs
Copy link

inJs commented Jan 19, 2016

        Object.prototype.hash = function() {
            var firstTime = true,
                ary,
                deep,
                cursor = 1,
                buf;

            return function(p) {
                if(firstTime) {
                    ary = p.split('.');
                    deep = ary.length;
                }

                if((!firstTime && (cursor === deep)) || (deep === 1)) {
                    firstTime = true;
                    cursor = 1;
                    return this[p];
                }

                var _p = firstTime ? ary.shift() : p;

                if(!this[_p]) {
                    firstTime = true;
                    cursor = 1;
                    return false;
                }

                buf = this[_p];

                firstTime && (firstTime = false);

                cursor++;

                return arguments.callee.call(buf ,ary.shift());
            }
        }.call();

@VaJoy
Copy link
Member Author

VaJoy commented Jan 19, 2016

2333,来个简单有趣直接的~

    Object.prototype.hash = function(attr){
      var obj = this;
      try{
        return new Function('obj', 'return obj.'+ attr)(this);
      }catch(e){
        return undefined
      }
    };

我这个比较简单,但是使用了new Function()性能上并不推荐使用(类似eval需要二次编译)
@LittleBearBond 的写的不错,不过可惜ES5的数组遍历方法都没法在内部提前停止迭代,如果要检索的属性很深的话,这种迭代性能上来说并不好。
个人建议用常规可内部截断迭代的循环(比如for或者es6的for...of)来处理是最好的

@inJs
Copy link

inJs commented Jan 20, 2016

@VaJoy @LittleBearBond 提个问题, 假如这种情况呢?

var o = {
   a: {
      b : false; 
   }
}

o.hash('a.b')

@VaJoy
Copy link
Member Author

VaJoy commented Jan 20, 2016

@inJs 那就返回false

@kitebear
Copy link
Member

Object.prototype.hash = function(attr){
    if(!attr || attr.length === 0){
        return (this.toString() === '[object Object]') ? this : this.toString();
    }

    if(toString.call(attr) === '[object String]'){
        attr = attr.split('.');
    }

    return this.hash.call(this[attr.shift()],attr);
};

@kitebear
Copy link
Member

@VaJoy 66666666

@liamwang
Copy link

我建议专门做一个代码出题的网站。

@Yoomin233
Copy link

Object.prototype.hash = function (attr) {
    let attrArr = attr.split('.')
    let res = this
    for (let i = 0, l = attrArr.length; i < l; i++) {
        if (res[attrArr[i]] === undefined) {
            return undefined
        } else {
            res = res[attrArr[i]]
        }
    }
    return res
}

比较直白的解法...
楼上递归解也不错

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants