静态属性 in JavaScript
静态属性分配给类函数本身,而不是分配给其原型属性。无需实例化任何对象即可直接调用这些属性。
以下是 JavaScript 中静态属性的代码 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>Static Properties in JavaScript</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to the access the static property school of Student</h3> <script> let resEle = document.querySelector(".result"); function Student(name, age) { this.name = name; this.age = age; } Student.school = "St Marks Public School"; document.querySelector(".Btn").addEventListener("click", () => { resEle.innerHTML = "Student.school = " + Student.school; }); </script> </body> </html>
输出
在单击“点击此处”按钮后 −
广告