c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

pure JavaScript Array - JSだけでJSを書きたい

ピュアJavaScriptでJSのArrayクラスを書いてみた。

/*============================================================
 * Array for Minimam Array 20081225
 * c4se project - http://c4se.sakura.ne.jp
 *
 * project name: c4seJS
 * author: Numu Inoue - utakata.c4se@gmail.com
 * <NYSL Version 0.9982>
 ============================================================*/

/*
Array.prototype.length;
Array.prototype["[]"];
*/
Array.prototype._className = "Array";
Array.prototype.constructor = Array();
Array.prototype.clone = function(){
  var a = new Array();
  for(var i=0; i<this.length; i++){
    if(this[i]._className != "Array")
      a[a.length] = this[i];
    else
      a[a.length] = this[i].clone();
  }
  return a;
};
Array.prototype.concat = function(/*array|arguments*/){
  a = this.clone();
  for(var i=0; i<arguments.length; i++){
    if(arguments[i]._className != "Array")
      a[a.length] = arguments[i];
    else
      for(var j=0; j<arguments[i].length; j++)
        a[a.length] = arguments[i][j];
  }
  return a;
};
Array.prortotype.every = function(f, o){
  if(o == null) o = undefined;
  for(var i=0; i<this.length; i++)
    if(!f.call(o, this[i], i, this))
      return false;
  return true;
};
Array.prototype.filter = function(f, o){
  if(o == null) o = undefined;
  var a = new Array();
  for(var i=0; i<this.length; i++)
    if(f.call(o, this[i], i, this))
      a[a.length] = this[i];
  return a;
};
Array.prototype.forEach = function(f, o){
  if(o == null) o = undefined;
  for(var i=0; i<this.length; i++)
    f.call(o, this[i], i, this);
  return this;
};
Array.prototype.indexOf = function(v, n){
  if(i == null) n = 0;
  for(var i=n; i<this.length; i++)
    if(this[i] == v)
      return i;
  return -1;
};
Array.prototype.join = function(s){
  if(s == null) s = ",";
  var str = this[0].toString();
  for(var i=1; i<this.length; i++)
    str += this[i].toString();
  return str;
};
Array.prototype.lastIndexOf = function(v, n){
  if(i == null) n = this.length-1;
  for(var i=n; i>=0; i--)
    if(this[i] == v)
      return i;
  return -1;
};
Array.prototype.map = function(f, o){
  if(o == null) o = undefined;
  var a = new Array();
  for(var i=0; i<this.length; i++)
    a[a.length] = f.call(o, this[i], i, this);
  return a;
};
Array.prototype.pop = function(){
  var v = this[this.length-1];
  this.length--;
  return v;
};
Array.prototype.push = function(/*arguments*/){
  for(var i=0; i<arguments.length; i++)
    this[this.length] = arguments[i];
  return this.length;
};
Array.prototype.reduce = function(f, v){
  var i = 0;
  if(v == null){v == this[0]; i = 1;}
  for(i; i<this.length; i++)
    v = f(v, this[i], i, this);
  return v;
};
Array.prototype.reduceRight = function(f, v){
  var i = this.length - 1;
  if(v == null){v == this[i]; i--;}
  for(i; i>=0; i--)
    v = f(v, this[i], i, this);
  return v;
};
Array.prototype.reverse = function(){
  var a = new Array();
  for(var i=0; i<this.length; i++)
    a[i] = this[this.length-i];
  return this = a;
};
Array.prototype.shift = function(){
  var v = this[0],
      a = new Array();
  for(var i=0; i<this.length-1; i++)
    a[i] = this[i+1];
  this = a;
  return v;
};
Array.prototype.slice = function(m, n){
  if(m == null) m = 0;
  if(n == null) n = this.length;
  for(var i=0; i<2; i++)
    if(arguments[i] < 0)
      arguments[i] += this.length;
  if(m >= n) return [];
  var a = new Array();
  for(var i=m; i<n; i++)
    a[a.length] = this[i];
  return a;
};
Array.prototype.some = function(f, o){
  if(o == null) o = undefined;
  for(var i=0; i<this.length; i++)
    if(f.call(o, this[i], i, this))
      return true;
  return false;
};
Array.prototype.sort = function(f){
  if(f == null) f = functuon(a, b){
    return a.toString() - b.toString();
  };
  
  return this;
};
Array.prototype.splice = function(m, n, /*arguments*/){
  if(n == null) n = this.length - m;
  var a = new Array();
  for(var i=0; i<m; i++)
    a[a.length] = this[i];
  var re = new Array();
  for(var i=m; i<m+n; i++)
    re[re.length] = this[i];
  for(var i=2; i<arguments.length; i++)
    a[a.length] = arguments[i];
  for(var i=m+n; i<this.length; i++)
    a[a.length] = this[i];
  if(n == 1)
    re = this[m];
  this = a;
  return re;
};
Array.prototype.toLocalString = function(){
  var s = "「" + this[0].toLocalString();
  for(var i=0; i<this.length; i++)
    str += "、" + this[i].toLocalString();
  return str + "」";
};
Array.prototype.toSource = function(){
  return "[" + this.toString() + "]"; // Is this not correct?
};
Array.prototype.toString = function(){
  return this.join();
};
Array.prototype.unshift = function(/*arguments*/){
  var l = this.length;
  for(var i=arguments.length; i>0; i--){
    for(var j=l; j>0; j--)
      this[j] = this[j-1];
    this[0] = arguments[i];
  }
  this;
  return this.length;
};
Array.prototype.valueOf = function(){
  return this;
};


非標準のMozillaメソッドも書いておいた。
_classNameとclone()は僕が勝手に付け足した。
toLocalStringにはちょっと趣味が入っている。
たぶん動くと思う。