25.類的靜態成員
阿新 • • 發佈:2022-01-02
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// function Phone() {}
// Phone.name = "手機"; // Phone.change = function () { // console.log("改變自己,改變全世界"); // };
// Phone.prototype.size = "666";
// let nokia = new Phone(); // console.log(nokia.name); 例項物件和函式物件自身的屬性是不相通的 // console.log(nokia.size);
class Phone { // 靜態屬性,static關鍵字表示這個成員屬於類本身,而不屬於例項物件,和ES5類似 static name = "手機"; static change() { console.log("改變自己,改變全世界"); } }
let apple = new Phone(); console.log(apple.name); console.log(Phone.name); </script> </body> </html>
// Phone.name = "手機"; // Phone.change = function () { // console.log("改變自己,改變全世界"); // };
// Phone.prototype.size = "666";
// let nokia = new Phone(); // console.log(nokia.name); 例項物件和函式物件自身的屬性是不相通的 // console.log(nokia.size);
class Phone { // 靜態屬性,static關鍵字表示這個成員屬於類本身,而不屬於例項物件,和ES5類似 static name = "手機"; static change() { console.log("改變自己,改變全世界"); } }
let apple = new Phone(); console.log(apple.name); console.log(Phone.name); </script> </body> </html>