<script>/*jsxjavascript + xmlhtmla1 xml他的标签是自定义的*//* Studentname,age*/class Student{/* 构造函数 --1、概念:构造一个对象的函数2、什么时候调用: new关键实例化一个对象的时候就调用3、在constructor中,this指向实例化的对象*/constructor(name,age){console.log("c")this.name = name;this.age = age;}sayName(){/* 普通方法中this指向实例化的对象 */console.log(this.name)}}var s = new Student("cheng",19);s.sayName();</script>
静态属性
<script>/*1、使用static关键字修饰的变量,叫静态属性。只能通过类名来调用2、使用static关键字修饰的方法,叫静态方法*/class Http{static baseUrl = "https://www.yuque.com"static request(){console.log(this)console.log("request")}}var h = new Http();console.log(Http.baseUrl)console.log(h.baseUrl)</script>
静态方法
<script>/*1、使用static关键字修饰的方法,叫静态方法a、只能通过类名去调用b、静态方法中的this指向class类*/class Http{static baseUrl = "https://www.yuque.com"static request(){console.log(this)console.log("request")}}var h = new Http();Http.request()h.request()</script>
静态方法和普通方法
<!--1、静态方法中不能直接使用普通方法2、普通方法中也不能直接使用静态方法--><script>class Http{static request(){console.log("request")sayName();}sayName(){request();console.log("sayName")}}// Http.request()var p = new Http();p.sayName()</script>
<script>/*静态方法之间的相互调用,在一个静态方法中调用另外一个静态方法,方法前面必须加上this关键字*/class Http{static request(){console.log("request")}static sayName(){this.request();console.log("sayName")}}Http.sayName();</script>
<!--一定要在普通方法中调用静态方法,可以在静态方法前面加上类名--><script>class Http{static request(){console.log("request")}sayName(){Http.request();console.log("sayName")}}var h = new Http();h.sayName();</script>
<!-- 在静态方法中调用普通方法 --><script>class Http{static request(){(new Http()).sayName();console.log("request")}sayName(){console.log("sayName")}}Http.request();</script>
