[JavaScript] Learning Advanced JavaScript 筆記

  • 2143
  • 0

摘要:[JavaScript] Learning Advanced JavaScript 筆記

Learning Advanced JavaScript,作者:John Resig,很棒的教學文章!順手記些自己做的實驗程式碼…在 firebug的主控台執行。

//#2: Goal: To be able to understand this function:
Function.prototype.bind = function(){ 
  var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); 
  return function(){ 
    return fn.apply(object, 
      args.concat(Array.prototype.slice.call(arguments))); 
  }; 
};

//my test note
var a=[1,2,3];
var b=[4,5,6];
console.log('concat, ',a.concat(b))
console.log(a)
console.log('pop, ',a.pop())
console.log(a)
console.log('shift, ',a.shift())
console.log(a)
console.log('slice, ', Array.prototype.slice.call(b,1))
console.log(b)
console.log('splice, ', Array.prototype.splice.call(b,1))
console.log(b)
/* result
concat,  [1, 2, 3, 4, 5, 6]
[1, 2, 3]
pop, 3
[1, 2]
shift, 1
[2]
slice, [5, 6]
[4, 5, 6]
splice, [5, 6]
[4]
*/

//#90: How method overloading might work, using the function length property.
function addMethod(object, name, fn){ 
  // Save a reference to the old method 
  var old = object[ name ]; 
 
  // Overwrite the method with our new one 
  object[ name ] = function(){ 
    // Check the number of incoming arguments, 
    // compared to our overloaded function 
    if ( fn.length == arguments.length ) 
      // If there was a match, run the function 
      return fn.apply( this, arguments ); 
 
    // Otherwise, fallback to the old method 
    else if ( typeof old === "function" ) 
      return old.apply( this, arguments ); 
  }; 
} 
 
function Ninjas(){ 
  var ninjas = [ "Dean Edwards", "Sam Stephenson", "Alex Russell" ]; 
  addMethod(this, "find", function(){ 
    return ninjas; 
  }); 
  addMethod(this, "find", function(name){ 
    var ret = []; 
    for ( var i = 0; i < ninjas.length; i++ ) 
      if ( ninjas[i].indexOf(name) == 0 ) 
        ret.push( ninjas[i] ); 
    return ret; 
  }); 
  addMethod(this, "find", function(first, last){ 
    var ret = []; 
    for ( var i = 0; i < ninjas.length; i++ ) 
      if ( ninjas[i] == (first + " " + last) ) 
        ret.push( ninjas[i] ); 
    return ret; 
  }); 
} 
 
var ninjas = new Ninjas(); 
assert( ninjas.find().length == 3, "Finds all ninjas" ); 
assert( ninjas.find("Sam").length == 1, "Finds ninjas by first name" ); 
assert( ninjas.find("Dean", "Edwards").length == 1, "Finds ninjas by first and last name" ); 
assert( ninjas.find("Alex", "X", "Russell") == null, "Does nothing" );

//my test note
function addMethod(object, name, fn){
  // Save a reference to the old method
  var old = object[ name ];
console.log(old,arguments.length,',',fn.length)
  // Overwrite the method with our new one
  object[ name ] = function(){
console.log(arguments.length,',',fn.length)
    // Check the number of incoming arguments,
    // compared to our overloaded function
    if ( fn.length == arguments.length )
      // If there was a match, run the function
      return fn.apply( this, arguments );

    // Otherwise, fallback to the old method
    else if ( typeof old === "function" )
      return old.apply( this, arguments );//closure
  };
}

function Ninjas(){
  var ninjas = [ "Dean Edwards", "Sam Stephenson", "Alex Russell" ];
  addMethod(this, "find", function(){
    return ninjas;
  });
  addMethod(this, "find", function(name){
    var ret = [];
    for ( var i = 0; i < ninjas.length; i++ )
      if ( ninjas[i].indexOf(name) == 0 )
        ret.push( ninjas[i] );
    return ret;
  });
  addMethod(this, "find", function(first, last){
    var ret = [];
    for ( var i = 0; i < ninjas.length; i++ )
      if ( ninjas[i] == (first + " " + last) )
        ret.push( ninjas[i] );
    return ret;
  });
}

var ninjas = new Ninjas();
console.log(ninjas)
console.log( ninjas.find().length == 3, 'ninjas.find() Finds all ninjas' );
console.log( ninjas.find("Sam").length == 1, 'ninjas.find("Sam") Finds ninjas by first name' );
console.log( ninjas.find("Dean", "Edwards").length == 1, 'ninjas.find("Dean", "Edwards") Finds ninjas by first and last name' );
console.log( ninjas.find("Alex", "X", "Russell") == null, 'ninjas.find("Alex", "X", "Russell") Does nothing' );
/* result
undefined 3 , 0
function() 3 , 1
function() 3 , 2
Object
0 , 2
0 , 1
0 , 0
true ninjas.find() Finds all ninjas
1 , 2
1 , 1
true ninjas.find("Sam") Finds ninjas by first name
2 , 2
true ninjas.find("Dean", "Edwards") Finds ninjas by first and last name
3 , 2
3 , 1
3 , 0
true ninjas.find("Alex", "X", "Russell") Does nothing
*/