JS中建立函数的几种方式

  1. 使用 "函数声明" 建立函数函数

    语法:
            *         function 函数名([形参1,形参2...形参N]){
            *             语句...
            *         }
*/

         function fun2(){
                console.log("这是个人第二个函数~~~");
                alert("哈哈哈哈哈");
                document.write("~~~~(>_<)~~~~");
            }
  1. 使用 "函数表达式" 来建立一个函数this

    var 函数名  = function([形参1,形参2...形参N]){
            *      语句....
            *  }
(可分两种):
           
           // 1.命名函数表达式
           var testA=function abc(){
                      console.log(aaa);
                    }
        // 2.匿名函数表达式  --最经常使用简称函数表达式
            var testB=function(){
                      console.log(bbb);
                       }
  1. 使用 "构造函数"code

    * 构造函数的执行流程:
            *     1.马上建立一个新的对象
            *     2.将新建的对象设置为函数中this,在构造函数中能够使用this来引用新建的对象
            *     3.逐行执行函数中的代码
            *     4.将新建的对象做为返回值返回
            * 
            * 使用同一个构造函数建立的对象,咱们称为一类对象,也将一个构造函数称为一个类。
            *     咱们将经过一个构造函数建立的对象,称为是该类的实例
function Person(name , age , gender){
            this.name = name;
            this.age = age;
            this.gender = gender;
            this.sayName = function(){
                alert(this.name);
            };
        }
        
        function Dog(){
            
        }
        
        var per = new Person("黎",18,"女");
相关文章
相关标签/搜索