DOMTokenList って初耳! でもって、contains(), item(), add(), remove(), toggle() を書いてみた。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title></title>
<style type="text/css">

</style>

<p id="a" class="ab cd ef gh ij">abc</p>

<script type="text/javascript">

//_______________________________

function TokenList (e, type) {
  this.e = e;
  this.at = type;
}

//____

TokenList.prototype.contains = (function () {
  return function ( name ) {
    var cnt, key, keys, str;
    
    str = '\u0020' + this.e[ this.at ] + '\u0020';
    if ( -1 < str.indexOf( '\u0020' + name + '\u0020' ) ) {
      return true;
    }

    cnt = 0;
    keys = name.split( /\u0020+/ );
    while( key = keys[ cnt++ ] ) {
      if( -1 == str.indexOf( '\u0020' + key + '\u0020' ) ) {
        return false;
      }
    }
    return true;
  };
})();

//____

TokenList.prototype.add = (function () {
  return function ( name ) {
    var cnt, key, keys, str, buf = [ ];
    
    keys = name.split( /\u0020+/ );
    str = '\u0020' + this.e[ this.at ] + '\u0020';
    cnt = 0;
    while( key = keys[ cnt++ ] ) {
      if( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
        buf.push( key );
      }
    }
    this.e[ this.at ] += '\u0020' + buf.join( '\u0020' );
    return buf;
  };
})();

//____

TokenList.prototype.item = (function () {
  return function ( num ) {
    var item = this.e[ this.at ].split( /\u0020+/ );
    return item ? item[ num ]: null;
  };
})();

//____

TokenList.prototype.length = (function () {
  return function ( ) {
    return this.e[ this.at ].split( /\u0020+/ ).length;
  };
})();

//____

TokenList.prototype.remove = (function () {
  return function ( name ) {
    var key, keys, str, cnt = 0, buf = [ ];
    
    keys = this.e[ this.at ].split( /\u0020+/ );
    str  = '\u0020' + name + '\u0020';
    
    while( key = keys[ cnt++ ] ) {
      if( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
        buf.push( key );
      }
    }
    this.e[ this.at ] = buf.join( '\u0020' );
    return buf;
  };
})();

//____

TokenList.prototype.toggle = (function () {
  return function ( name ) {
    var key, keys, str, cnt = 0, buf = [ ];
    
    keys = name.split( /\u0020+/ );
    str  = '\u0020' + this.e[ this.at ] + '\u0020';

    while( key = keys[ cnt++ ] ) {
      if ( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
        str += key + '\u0020';
      } else {
        buf.push( key );
      }
    }
        
    this.e[ this.at ] = str;
    if( buf.length ) this.remove( buf.join( '\u0020' ) );
    return this.e[ this.at ];
  };
})();

//______________________________________

var a = { 'classList': new TokenList( document.getElementById('a'), 'className') };
// var a = new TokenList( document.getElementById('a'), 'className', 'classList');//可能?

alert(document.getElementById('a').className);
a.classList.toggle( 'cd gh' );
alert(document.getElementById('a').className);
a.classList.toggle( 'cd gh' );
alert(document.getElementById('a').className);

</script>