nunnly / everycode

Javascript 每日一练
116 stars 26 forks source link

2014年11月19日 #5

Open nunnly opened 10 years ago

nunnly commented 10 years ago

现在你有一个复杂的多重嵌套的对象,但是你莫名蛋疼,突然就不想用if obj.property == null这个方法。于是乎打算,在Object的原型上创建一个方法(prototype method),传递一个路径,返回undefined 或 值(value);


Object.prototype.hash = function(path){

}

var obj = {
  person: {
    name: 'joe',
    history: {
      hometown: 'bratislava',
      bio: {
        funFact: 'I like fishing.'
      }
    }
  }
};

obj.hash('person.name'); // 'joe'
obj.hash('person.history.bio'); // { funFact: 'I like fishing.' }
obj.hash('person.history.homeStreet'); // undefined
obj.hash('person.animal.pet.needNoseAntEater'); // undefined
XadillaX commented 10 years ago
Object.prototype.hash = function(name) {
    try {
        return new Function("a", "return a." + name + ";")(this);
    } catch(e) {
        return undefined;
    }
};
xinglie commented 10 years ago
var obj = {
  person: {
    name: 'joe',
    history: {
      hometown: 'bratislava',
      bio: {
        funFact: 'I like fishing.'
      }
    }
  }
};
Object.prototype.hash=function(key){
  var o=this;
  if(key){
      var keys=(key+'').split('.');
      while (o && keys[0]) {
        o = o[keys.shift()];
      }
      if (keys[0]) {
        o = undefined;
      }
  }
  return o;
};
console.log(obj.hash('person.name')); // 'joe'
console.log(obj.hash('person.history.bio')); // { funFact: 'I like fishing.' }
console.log(obj.hash('person.history.homeStreet')); // undefined
console.log(obj.hash('person.animal.pet.needNoseAntEater')); // undefined
weisuiyu commented 10 years ago
Object.prototype.hash = function(u){
    try{
        eval('var s = this.'+u)
    }catch(e){}
    return s;
}
XadillaX commented 10 years ago

上一个我同事的:

Object.prototype.hash = function(path) {
  return path.split('.').reduce(function(obj, key){ return obj && obj[key]; }, this);
};
nunnly commented 10 years ago

@XadillaX 你这同事的,真心赞!

businiaowa commented 10 years ago
Object.prototype.hash = function( path ) {  
        path = path + "";
        var propertyArr = path.split("."), property;
        var value = this;
        if( !value[path] ) {                    
            while(property = propertyArr.shift()) {
                if(value[property]) 
                    value = value[property];
                else
                    return undefined;
            }
            return value;
        }

        return value[path];

    }
xinglie commented 10 years ago

@businiaowa 要考虑这样的情况:

obj:{ a:0 }

然后 obj.hash('a'); 你看一下

businiaowa commented 10 years ago

@xinglie 噢对

fireflyhoo commented 10 years ago

Object.prototype.hash = function( path ){ if(path == ''){ return undefined; } var paths = path.split("."); if (paths.length > 1) { return this[paths[0]] ? this[paths[0]].hash(paths.slice(1).join(".")): undefined }else{ return this[paths[0]]; } }

qingo commented 9 years ago

最近在写代码是发现hash方法还是很常用的,但是这个题目只是getter,我填一个自己写做了一点小变化的,jQuery风格的,gettersetter根据参数的不同而运行的代码,感谢 XadillaX 及其同事。

function hash(object, name, value) {
  var paths = name.split('.'), len = paths.length;
  return paths.reduce(function (prev, current, index) {
    if(value){
      return prev[current] = (index === len - 1 ? value : {});
    }else {
      return prev[current]
    }
  }, object);
}
VaJoy commented 9 years ago

就是死不要脸的要使用eval,哼 →。→

    Object.prototype.getName = function(context){
      context = context||window;
      for(var key in context){
        if(context[key]===this) return key;
      }
    };

    Object.prototype.hash = function(path){
      try{
        return eval(this.getName()+"."+path)
      }catch(e) {
        return undefined;
      }
    };