静态方法:直接添加到类上面 通过类名调用
实例方法:添加到这个类的原型上 通过实例调用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="../jquery/jquery-3.4.1.min.js"></script> <script> //1:定义一个类 function AClass(){ } //2:给这个类添加一个静态方法 //直接添加给类的就是静态方法 AClass.staticMethod=function(){ alert('staticMethod'); } //静态方法通过类名调用 AClass.staticMethod(); //3:给这个类添加一个实例方法 原型上 AClass.prototype.instanceMethod=function(){ alert('instanceMethod'); } //实例方法通过类的实例调用 //创建一个实例(对象) var a=new AClass(); //通过实例调用实例方法 a.instanceMethod(); </script> </head> <body> </body> </html>
前端学习——js——静态方法和实例方法
最新推荐文章于 2022-11-18 15:43:20 发布