Hướng dẫn static variable in javascript - biến tĩnh trong javascript

Từ khóa static xác định một phương thức hoặc thuộc tính tĩnh cho một lớp hoặc khối khởi tạo tĩnh lớp (xem liên kết để biết thêm thông tin về việc sử dụng này). Cả các phương thức tĩnh và tính chất tĩnh đều không thể được gọi trên các trường hợp của lớp. Thay vào đó, họ được gọi vào chính lớp.static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself. static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

Nội dung chính ShowShow

  • Sử dụng các thành viên tĩnh trong các lớp học
  • Gọi các thành viên tĩnh từ một phương thức tĩnh khác
  • Gọi các thành viên tĩnh từ một hàm tạo lớp và các phương thức khác
  • Thông số kỹ thuật
  • Tính tương thích của trình duyệt web
  • Các lớp có thể có các biến tĩnh?
  • Một lớp học có thể tĩnh trong javascript?
  • Chúng ta có thể sử dụng biến tĩnh trong JavaScript không?
  • Tôi có thể khai báo biến trong lớp JavaScript không?

Các phương thức tĩnh thường là các hàm tiện ích, chẳng hạn như các hàm để tạo hoặc nhân bản các đối tượng, trong khi các thuộc tính tĩnh rất hữu ích cho bộ nhớ cache, cấu hình cố định hoặc bất kỳ dữ liệu nào khác mà bạn không cần được sao chép trên các trường hợp.

Lưu ý: Trong bối cảnh của các lớp, Nội dung tài liệu web MDN sử dụng các thuộc tính và trường thuật ngữ có thể thay thế cho nhau. In the context of classes, MDN Web Docs content uses the terms properties and fields interchangeably. In the context of classes, MDN Web Docs content uses the terms properties and fields interchangeably.

Thử nó

Cú pháp

static methodName() { /* … */ }
static propertyName [= value];

// Class static initialization block
static {

}

Ví dụ

Sử dụng các thành viên tĩnh trong các lớp học

Gọi các thành viên tĩnh từ một phương thức tĩnh khác

  1. Gọi các thành viên tĩnh từ một hàm tạo lớp và các phương thức khác
  2. Thông số kỹ thuật
  3. Tính tương thích của trình duyệt web
class Triple {
  static customName = 'Tripler';
  static description = 'I triple any number you provide';
  static calculate(n = 1) {
    return n * 3;
  }
}

class SquaredTriple extends Triple {
  static longDescription;
  static description = 'I square the triple of any number you provide';
  static calculate(n) {
    return super.calculate(n) * super.calculate(n);
  }
}

console.log(Triple.description);            // 'I triple any number you provide'
console.log(Triple.calculate());            // 3
console.log(Triple.calculate(6));           // 18

const tp = new Triple();

console.log(SquaredTriple.calculate(3));    // 81 (not affected by parent's instantiation)
console.log(SquaredTriple.description);     // 'I square the triple of any number you provide'
console.log(SquaredTriple.longDescription); // undefined
console.log(SquaredTriple.customName);      // 'Tripler'

// This throws because calculate() is a static member, not an instance member.
console.log(tp.calculate());                // 'tp.calculate is not a function'

Gọi các thành viên tĩnh từ một phương thức tĩnh khác

Gọi các thành viên tĩnh từ một hàm tạo lớp và các phương thức khác

class StaticMethodCall {
  static staticProperty = 'static property';
  static staticMethod() {
    return `Static method and ${this.staticProperty} has been called`;
  }
  static anotherStaticMethod() {
    return `${this.staticMethod()} from another static method`;
  }
}
StaticMethodCall.staticMethod();
// 'Static method and static property has been called'

StaticMethodCall.anotherStaticMethod();
// 'Static method and static property has been called from another static method'

Thông số kỹ thuật

Tính tương thích của trình duyệt web

Các lớp có thể có các biến tĩnh?

Thông số kỹ thuật

Tính tương thích của trình duyệt web
Các lớp có thể có các biến tĩnh?
# sec-class-definitions

Tính tương thích của trình duyệt web

Các lớp có thể có các biến tĩnh?

Một lớp học có thể tĩnh trong javascript?

Các lớp có thể có các biến tĩnh?

Một lớp học có thể tĩnh trong javascript?, and they are declared outside a method, with the help of the keyword 'static'. Static variable is the one that is common to all the instances of the class. A single copy of the variable is shared among all objects.

Một lớp học có thể tĩnh trong javascript?

Chúng ta có thể sử dụng biến tĩnh trong JavaScript không? (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

Chúng ta có thể sử dụng biến tĩnh trong JavaScript không?

Tôi có thể khai báo biến trong lớp JavaScript không?. The value of the static variable can be reassigned, unlike the constant variable. Why we create a static variable in JavaScript: We create a static variable in JavaScript to prevent replication, fixed-configuration, and it is also useful for caches.

Tôi có thể khai báo biến trong lớp JavaScript không?

Các phương thức tĩnh thường là các hàm tiện ích, chẳng hạn như các hàm để tạo hoặc nhân bản các đối tượng, trong khi các thuộc tính tĩnh rất hữu ích cho bộ nhớ cache, cấu hình cố định hoặc bất kỳ dữ liệu nào khác mà bạn không cần được sao chép trên các trường hợp.it needs to be a property of the class or, as you did so, scoped within a method in the class. It's all about scoping and variables are not supported in the scope definition of a class.