zoukankan      html  css  js  c++  java
  • js中类定义函数时用prototype与不用的区别

    转载自:https://blog.csdn.net/yexudengzhidao/article/details/72866047

    先看例子

    function ListCommon2(first,second,third) 
    { 
        this.First=function () 
        { 
            alert("first do"+first); 
        } 
    } 
    //不加prototype的情况
    ListCommon2.do1=function(first) 
    { 
        // this.First(); 
        alert("first do"+first); 
    } 
    //添加prototype的情况
    ListCommon2.prototype.do2=function(first) 
    { 
        // this.First(); 
        alert("first do"+first); 
    } 

    上面添加与不添加prototype有什么区别呢?下面我们来测试这个实例。代码如下

    var t1=new ListCommon2("烧水1","泡茶1","喝1"); 
    // t1.do1();//调用出错 
    ListCommon2.do1("烧水1"); 
    var t2=new ListCommon2("烧水2","泡茶2","喝2"); 
    t2.do2("烧水2");// 
    // ListCommon2.do2("烧水1");//调用出错 

    经过测试发现,没有使用prototype的方法相当于类的静态方法,因此可以这样调用,ListCommon2.do1(“烧水1”);但如果这样调用就会出错,t1.do1();

    相反,使用prototype的方法相当于类的实例方法,不许new后才能使用,ListCommon2.do2(“烧水1”);这样就会出错

    结论:

    • 使用 prototype定义的方法相当于类的实例方法,必须new后才能使用
    • 不使用prototype定义的方法相当于类的静态方法,可以直接使用,不需要new
  • 相关阅读:
    练习二十七:递归函数应用
    mysql8.0数据库忘记密码时进行修改方法
    格式化字符串两种方式
    练习二十六:阶乘计算(递归)
    练习二十五:阶乘之和计算
    Dapper批量添加
    c# FTP操作类(转)
    c# 依赖注入之---反射(转)
    c# 依赖注入之---setterInjection(转)
    php遍历数组赋值
  • 原文地址:https://www.cnblogs.com/heben/p/8989554.html
Copyright © 2011-2022 走看看