JSの関数定義&オブジェクト作成の3つの方法


JS関数作成の3つの方法JS作成のオブジェクト3つの方法
一、javaScript関数で作成された3つの方法



The three ways 

/*


   、function        :
    function    (      ) {
       
    }
         :   ();
*/
window.onload=function(){
    alert("                   !!!");
    function voidFun(){
        alert("            ");
    }
    voidFun();//     voidFun();             voidFun()

//js            
    function voidFun(){
        alert("                               ");
    }


    function singleFun(a){
        alert("                    :" + a);
    }

     singleFun(666);

    function moreFun(a,b){
        alert("                  :"+a+"   "+b);
    }
    moreFun("  ",2016);
/*
    arguments   Java            (        )
    public void fun( Object ... params ){} params          

     function   arguments[i]          
    arguments           ,        。
                 ,arguments       
     JavaScript         ,          ,            
                  undefined
 */
    function sum(){
        //            
        var result = 0;
        //     arguments
        for (var i = 0; i < arguments.length; i++ ) {
            //          
            var p = arguments[i];

            alert(typeof(p)+"     "+p);

            if ( typeof(p) == "number" ) {
                result += p;
            }
        }
        return result;
    }
    var result = sum(12,88,"100",50);
    alert("  : "+result);



//   :var     = new Function(“    ”,”   ”);
//  :
var sum = new Function("num1,num2","return num1+num2");
    //     
    var result1 = sum(120,130,66);
    alert(result1);



//          :
// var     = function(    )  {     }
    var funname = function(a,b){

        alert(a);   // abc
        alert(b);   // true
    }
    //     
    funname("abc",true);
}



    

html javascript <head>

二、javaScript作成対象の3つの方法



new Object of js

/*
   、Object        
    var     = new Object();  //new      
       .    =  ;            //            
       .    = function(){}  //           
    js       :
      .   
    */
    var Student1=new Object();
    Student1.name="        ";
    Student1.age=23;
    //          toString              
    Student1.toString1=function(){
        alert("    hello my name is toString1()");
    }

    Student1.toString=function(){
    var result="      toString       :  :" + this.age + ",  :" + this.name;
    alert(result);

    return result="       toString                   ";
    }
    alert(Student1);
    Student1.toString1();
/*


      、{}           
        ,    ,      ,      。
    var     = {
       :  , //        
       (   ): function(){}  //        
    } 

     :
  .   
*/
        var Student2={
        name: "        2",
        age: 29,
    toString1:function(){
        alert("    my name is Student2 toString1()");

    },
    toString:function(){
        alert("     my name is Student2 toString()");
        return "       toString                     undefine";
    }
};
    alert( Student2.name );
    alert(Student2);

/*


   、function        ( js       )
    function    (){
    this.    =  ; //        
    this.    = function(){}  //       
    }

      
    var     = new    () //           
       .   
 */

 function Student3(){
        this.name="        3";
        this.age=30;
        this.toString=function(){
            alert("      Strudent3 toString()")
            return "       toString                     undefine";
        }
        this.toString1=function(){
            alert("       Strudent3 toString2()");
        }
    }
    var stu=new Student3();
     alert(stu);
     stu.toString1();