JavaScript学習ノート9 newとappy、call

1794 ワード

function logs(obj){document.write(obj+"<br/>");}
//new   
function Shape(type){
    this.type = type || "rect";
    this.calc = function (){
        return "calc,"+this.type;
    }
}
//   new            
    //  ,       
    //       apply   ,           apply       ,      。
    //        this             
var triangle1 = new Shape("triangle");
logs(triangle1.calc());
//             
var triangle2 = {};
Shape.apply(triangle2,["trangle2"]);
logs(triangle2.calc());

//  apply,call    ,
//foo.call(this, arg1,arg2,arg3) == foo.apply(this, arguments)==this.foo(arg1, arg2, arg3)

//call, apply   Function.prototype     ,  JavaScript       ,    Function.prototype,
//    Function    ,         call, apply  .         ,           
//     .           ,         ,        
//   :               
//   :         

//        call,          apply  arguments

//http://www.cnblogs.com/TomXu/archive/2012/02/10/2342098.html
//      
//1.                  
//2.          window   
//  1:

if (!("a" in window)) { var a = 1; }
logs(a);//undefined
//  2:
logs("a" in window);//true   a      ,
                    // JavaScript              ,              
var a;

//1.         ,                       
function functionName(arg1,arg2){}
//2.         ,           
var functionName = function(arg1,arg2){}
//3.           ,         
function value (){ return 1;}
var value; logs(typeof value);//function        
var value = 1; logs(typeof value);//number