1. 程式人生 > >Array.prototype.push結合apply 實現 object 偽裝陣列

Array.prototype.push結合apply 實現 object 偽裝陣列

var Visitor = {}
Visitor .push  =  function(){
    return Array.prototype.push.apply( this, arguments );
}
var obj = {};
obj.push = Visitor .push;
obj.push( '"first" );
alert ( obj[0] )  
//"first"
alert ( obj.length );