1.定义普通函数(常用)

//1.普通函数
        function defineFun1(p1, p2) {
            return p1 + p2;
        }

 

2.定义匿名函数(最常用)

//2.匿名函数
        var defineFun2 = function(p1, p2) {
            return p1 + p2;
        }

 

3.直接用Function的构造器来创建函数(少用)

//3.直接用Function的构造器来创建函数
        var defineFun3 = new Function("a", "b", "return a + b");

这种的定义方法就是new Function(p1, p2, p3, ..PN);

P1, P2一直到PN-1是函数的参数,PN是函数的执行体。

缺点:难检查语法错误

 

总结:其实定义了一个function就等于定义了一个同名的类。

例如下面的代码:

        alert(defineFun1 instanceof  Object); //true
        alert(defineFun1 instanceof  Function); //true
        alert(defineFun2 instanceof  Object); //true
        alert(defineFun2 instanceof  Function); //true

相关文章:

  • 2021-06-03
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2021-11-28
  • 2021-09-03
  • 2021-08-30
  • 2021-11-05
  • 2022-12-23
  • 2022-03-02
  • 2021-12-04
相关资源
相似解决方案