如何获取属性的值而不是 [Object object] 的值? (JavaScript)

How to get the value of attribute instead of an [Object object] ? (JavaScript)

无论我多么努力,我总是得到 [Object object] 或者正在制造冲突。 (我尝试了其他几种变体。);(

var $ = function(selector){

  var x; 
  var obj = {
  
    myLib(selector){
      return x || document.querySelectorAll(selector);
    },
    
    cl(selector){
      x = [x[0].closest(selector)];
      return this;
    },    
      
    attr(s,v){
      s&&v ? x.forEach(y=>{y.setAttribute(s,v);})
           : x = [x[0].getAttribute(s)]; x.value; // what is wrong here ?
      return this;
    },
      
  };
        
  x = obj.myLib(selector);
  return obj;
  
};

var wtf = $('.kilo').cl('.uniform').attr('data-wrestler');

console.log('WTF: '+wtf);
console.log('WTF: '+JSON.stringify(wtf));
<div id="foxtrott">
  foxtrott
  <div class="uniform" data-wrestler="hulkster">
    uniform
    <div class="charlie">
      charlie
      <div class="kilo">
        kilo
      </div>
    </div>
  </div>
</div>

如果您还有有用的链接来阅读有关此主题的内容,那就太好了。

提前致谢,伙计们!

你应该 return x 而不是 attr 方法中的 this

var $ = function(selector){

  var x; 
  var obj = {
  
    myLib(selector){
      return x || document.querySelectorAll(selector);
    },
    
    cl(selector){
      x = [x[0].closest(selector)];
      return this;
    },    
      
    attr(s,v){
      s&&v ? x.forEach(y=>{y.setAttribute(s,v);})
           : x = [x[0].getAttribute(s)]; 
      return x;
    },
      
  };
        
  x = obj.myLib(selector);
  return obj;
  
};

var wtf = $('.kilo').cl('.uniform').attr('data-wrestler');

console.log('WTF: '+wtf);
console.log('WTF: '+JSON.stringify(wtf));
<div id="foxtrott">
  foxtrott
  <div class="uniform" data-wrestler="hulkster">
    uniform
    <div class="charlie">
      charlie
      <div class="kilo">
        kilo
      </div>
    </div>
  </div>
</div>