Hướng dẫn how to delete key in object javascript - cách xóa khóa trong đối tượng javascript

Toán tử

delete identifier;
delete object.#privateProperty;
1 loại bỏ một thuộc tính khỏi một đối tượng. Nếu giá trị của tài sản là một đối tượng và không còn tài liệu tham khảo nào đến đối tượng, đối tượng được giữ bởi thuộc tính đó cuối cùng được phát hành tự động.
delete identifier;
delete object.#privateProperty;
1 operator
removes a property from an object. If the property's value is an object and there are no more references to the object, the object held by that property is eventually released automatically.

Thử nó

Cú pháp

delete object.property
delete object[property]

Lưu ý: Cú pháp cho phép phạm vi biểu thức rộng hơn theo toán tử

delete identifier;
delete object.#privateProperty;
1, nhưng chỉ các hình thức trên dẫn đến các hành vi có ý nghĩa. The syntax allows a wider range of expressions following the
delete identifier;
delete object.#privateProperty;
1 operator, but only the above forms lead to meaningful behaviors.

Thông số

delete identifier;
delete object.#privateProperty;
3

Tên của một đối tượng, hoặc một biểu thức đánh giá một đối tượng.

delete identifier;
delete object.#privateProperty;
4

Tài sản để xóa.

Giá trị trả về

delete identifier;
delete object.#privateProperty;
5 Đối với tất cả các trường hợp ngoại trừ khi tài sản là một thuộc tính không thể cấu hình riêng, trong trường hợp đó
delete identifier;
delete object.#privateProperty;
6 được trả về ở chế độ không nghiêm ngặt.

Ngoại lệ

delete identifier;
delete object.#privateProperty;
7

Ném ở chế độ nghiêm ngặt nếu tài sản là một thuộc tính không thể cấu hình riêng.

delete identifier;
delete object.#privateProperty;
8

Ném nếu

delete identifier;
delete object.#privateProperty;
3 là
delete console.log[1];
// Logs 1, returns true, but nothing deleted
0.

Sự mô tả

Toán tử

delete identifier;
delete object.#privateProperty;
1 có mức độ ưu tiên tương tự như các toán tử Unary khác như
delete console.log[1];
// Logs 1, returns true, but nothing deleted
2. Do đó, nó chấp nhận bất kỳ biểu thức nào được hình thành bởi các nhà khai thác cấp cao hơn. Tuy nhiên, các biểu mẫu sau dẫn đến các lỗi cú pháp sớm ở chế độ nghiêm ngặt:

delete identifier;
delete object.#privateProperty;

Bởi vì các lớp tự động ở chế độ nghiêm ngặt và các thuộc tính riêng chỉ có thể được tham chiếu hợp pháp trong các cơ quan lớp, điều này có nghĩa là các thuộc tính riêng tư không bao giờ có thể bị xóa. Mặc dù

delete console.log[1];
// Logs 1, returns true, but nothing deleted
3 có thể hoạt động nếu
delete console.log[1];
// Logs 1, returns true, but nothing deleted
4 đề cập đến một thuộc tính có thể định cấu hình của đối tượng toàn cầu, bạn nên tránh biểu mẫu này và thay vào đó là
delete console.log[1];
// Logs 1, returns true, but nothing deleted
5.

Mặc dù các biểu thức khác được chấp nhận, chúng không dẫn đến các hành vi có ý nghĩa:

delete console.log[1];
// Logs 1, returns true, but nothing deleted

Toán tử

delete identifier;
delete object.#privateProperty;
1 loại bỏ một thuộc tính nhất định khỏi một đối tượng. Khi xóa thành công, nó sẽ trả lại
delete identifier;
delete object.#privateProperty;
5, nếu không
delete identifier;
delete object.#privateProperty;
6 sẽ được trả lại. Không giống như những gì niềm tin phổ biến cho thấy [có lẽ do các ngôn ngữ lập trình khác như xóa trong C ++], toán tử
delete identifier;
delete object.#privateProperty;
1 không liên quan gì đến bộ nhớ giải phóng trực tiếp. Quản lý bộ nhớ được thực hiện gián tiếp thông qua các tài liệu tham khảo. Xem trang quản lý bộ nhớ để biết thêm chi tiết.nothing to do with directly freeing memory. Memory management is done indirectly via breaking references. See the memory management page for more details.

Điều quan trọng là phải xem xét các kịch bản sau:

  • Nếu tài sản mà bạn đang cố gắng xóa không tồn tại,
    delete identifier;
    delete object.#privateProperty;
    
    1 sẽ không có bất kỳ ảnh hưởng nào và sẽ trả lại
    delete identifier;
    delete object.#privateProperty;
    
    5.
  • delete identifier;
    delete object.#privateProperty;
    
    1 chỉ có ảnh hưởng đến các thuộc tính riêng. Nếu một thuộc tính có cùng tên tồn tại trên chuỗi nguyên mẫu của đối tượng, thì sau khi xóa, đối tượng sẽ sử dụng thuộc tính từ chuỗi nguyên mẫu.
  • Các thuộc tính không thể cấu hình không thể được loại bỏ. Điều này bao gồm các thuộc tính của các đối tượng tích hợp như
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    3,
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    4,
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    5 và các thuộc tính được tạo thành không thể cấu hình được với các phương thức như
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    6.
  • Xóa các biến, bao gồm các tham số chức năng, không bao giờ hoạt động.
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    7 sẽ ném
    // Creates the property empCount on the global scope.
    // Since we are using var, this is marked as non-configurable.
    var empCount = 43;
    
    // Creates the property adminName on the global scope.
    // Since it was defined without "var", it is marked configurable.
    EmployeeDetails = {
      name: "xyz",
      age: 5,
      designation: "Developer",
    };
    
    // delete can be used to remove properties from objects.
    delete EmployeeDetails.name; // returns true
    
    // Even when the property does not exist, delete returns "true".
    delete EmployeeDetails.salary; // returns true
    
    // EmployeeDetails is a property of the global scope.
    delete EmployeeDetails; // returns true
    
    // On the contrary, empCount is not configurable
    // since var was used.
    delete empCount; // returns false
    
    // delete also does not affect built-in static properties
    // that are non-configurable.
    delete Math.PI; // returns false
    
    function f[] {
      var z = 44;
    
      // delete doesn't affect local variable names
      delete z; // returns false
    }
    
    8 ở chế độ nghiêm ngặt và sẽ không có tác dụng trong chế độ không nghiêm ngặt.
    • Bất kỳ biến nào được khai báo với
      // Creates the property empCount on the global scope.
      // Since we are using var, this is marked as non-configurable.
      var empCount = 43;
      
      // Creates the property adminName on the global scope.
      // Since it was defined without "var", it is marked configurable.
      EmployeeDetails = {
        name: "xyz",
        age: 5,
        designation: "Developer",
      };
      
      // delete can be used to remove properties from objects.
      delete EmployeeDetails.name; // returns true
      
      // Even when the property does not exist, delete returns "true".
      delete EmployeeDetails.salary; // returns true
      
      // EmployeeDetails is a property of the global scope.
      delete EmployeeDetails; // returns true
      
      // On the contrary, empCount is not configurable
      // since var was used.
      delete empCount; // returns false
      
      // delete also does not affect built-in static properties
      // that are non-configurable.
      delete Math.PI; // returns false
      
      function f[] {
        var z = 44;
      
        // delete doesn't affect local variable names
        delete z; // returns false
      }
      
      9 đều không thể bị xóa khỏi phạm vi toàn cầu hoặc từ phạm vi của một hàm, bởi vì trong khi chúng có thể được gắn vào đối tượng toàn cầu, chúng không thể cấu hình được.
    • Bất kỳ biến nào được khai báo với
      function Foo[] {
        this.bar = 10;
      }
      
      Foo.prototype.bar = 42;
      
      const foo = new Foo[];
      
      // foo.bar is associated with the
      // own property.
      console.log[foo.bar]; // 10
      
      // Delete the own property within the
      // foo object.
      delete foo.bar; // returns true
      
      // foo.bar is still available in the
      // prototype chain.
      console.log[foo.bar]; // 42
      
      // Delete the property on the prototype.
      delete Foo.prototype.bar; // returns true
      
      // The "bar" property can no longer be
      // inherited from Foo since it has been
      // deleted.
      console.log[foo.bar]; // undefined
      
      0 hoặc
      function Foo[] {
        this.bar = 10;
      }
      
      Foo.prototype.bar = 42;
      
      const foo = new Foo[];
      
      // foo.bar is associated with the
      // own property.
      console.log[foo.bar]; // 10
      
      // Delete the own property within the
      // foo object.
      delete foo.bar; // returns true
      
      // foo.bar is still available in the
      // prototype chain.
      console.log[foo.bar]; // 42
      
      // Delete the property on the prototype.
      delete Foo.prototype.bar; // returns true
      
      // The "bar" property can no longer be
      // inherited from Foo since it has been
      // deleted.
      console.log[foo.bar]; // undefined
      
      1 đều không thể bị xóa khỏi phạm vi mà chúng được xác định, vì chúng không được gắn vào một đối tượng.

Ghi chú trình duyệt chéo

Theo đặc điểm kỹ thuật ECMAScript hiện đại, thứ tự truyền tải của các thuộc tính đối tượng được xác định rõ và ổn định trong các triển khai. Tuy nhiên, trong trường hợp của Internet Explorer, khi một người sử dụng

delete identifier;
delete object.#privateProperty;
1 trên một tài sản, một số kết quả hành vi khó hiểu, ngăn chặn các trình duyệt khác sử dụng các đối tượng đơn giản như chữ cái theo nghĩa đen như các mảng liên kết được đặt hàng. Trong Explorer, trong khi giá trị thuộc tính thực sự được đặt thành
function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
3, nếu sau đó thêm lại một thuộc tính có cùng tên, thuộc tính sẽ được lặp lại ở vị trí cũ của nó - không phải ở cuối chuỗi lặp như người ta có thể mong đợi sau khi xóa tài sản và sau đó thêm nó trở lại.

Nếu bạn muốn sử dụng một mảng liên kết theo thứ tự với sự hỗ trợ của thời gian chạy cũ, hãy sử dụng đối tượng

function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
4 nếu có [ví dụ: thông qua polyfill] hoặc mô phỏng cấu trúc này với hai mảng riêng biệt [một cho các phím và phần kia cho các giá trị] hoặc xây dựng một mảng các đối tượng tài sản đơn, v.v.

Ví dụ

Sử dụng xóa

Lưu ý: Ví dụ sau sử dụng các tính năng không nghiêm ngặt của chế độ, như tự nhiên tạo các biến toàn cầu và xóa các định danh, bị cấm ở chế độ nghiêm ngặt. The following example uses non-strict-mode only features, like implicitly creating global variables and deleting identifiers, which are forbidden in strict mode.

// Creates the property empCount on the global scope.
// Since we are using var, this is marked as non-configurable.
var empCount = 43;

// Creates the property adminName on the global scope.
// Since it was defined without "var", it is marked configurable.
EmployeeDetails = {
  name: "xyz",
  age: 5,
  designation: "Developer",
};

// delete can be used to remove properties from objects.
delete EmployeeDetails.name; // returns true

// Even when the property does not exist, delete returns "true".
delete EmployeeDetails.salary; // returns true

// EmployeeDetails is a property of the global scope.
delete EmployeeDetails; // returns true

// On the contrary, empCount is not configurable
// since var was used.
delete empCount; // returns false

// delete also does not affect built-in static properties
// that are non-configurable.
delete Math.PI; // returns false

function f[] {
  var z = 44;

  // delete doesn't affect local variable names
  delete z; // returns false
}

Xóa và chuỗi nguyên mẫu

Trong ví dụ sau, chúng tôi xóa một thuộc tính riêng của một đối tượng trong khi thuộc tính có cùng tên có sẵn trên chuỗi nguyên mẫu:

function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined

Xóa các yếu tố mảng

Khi bạn xóa một phần tử mảng, mảng

function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
5 không bị ảnh hưởng. Điều này giữ ngay cả khi bạn xóa phần tử cuối cùng của mảng.

Khi toán tử

delete identifier;
delete object.#privateProperty;
1 loại bỏ một phần tử mảng, phần tử đó không còn nằm trong mảng. Trong ví dụ sau,
function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
7 được xóa bằng
delete identifier;
delete object.#privateProperty;
1.

const trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log[3 in trees]; // false

Điều này tạo ra một mảng thưa thớt với một khe trống. Nếu bạn muốn một phần tử mảng tồn tại nhưng có giá trị không xác định, hãy sử dụng giá trị

function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
3 thay vì toán tử
delete identifier;
delete object.#privateProperty;
1. Trong ví dụ sau,
function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
7 được gán giá trị
function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
3, nhưng phần tử mảng vẫn tồn tại:

const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees[3] = undefined;
console.log[3 in trees]; // true

Nếu thay vào đó, bạn muốn xóa một phần tử mảng bằng cách thay đổi nội dung của mảng, hãy sử dụng phương thức

const trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log[3 in trees]; // false
3. Trong ví dụ sau,
function Foo[] {
  this.bar = 10;
}

Foo.prototype.bar = 42;

const foo = new Foo[];

// foo.bar is associated with the
// own property.
console.log[foo.bar]; // 10

// Delete the own property within the
// foo object.
delete foo.bar; // returns true

// foo.bar is still available in the
// prototype chain.
console.log[foo.bar]; // 42

// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true

// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log[foo.bar]; // undefined
7 bị xóa khỏi mảng hoàn toàn bằng cách sử dụng
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log[3 in trees]; // false
3:

const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees.splice[3, 1];
console.log[trees]; // ["redwood", "bay", "cedar", "maple"]

Xóa các thuộc tính không thể cấu hình được

Khi một tài sản được đánh dấu là không thể cấu hình được,

delete identifier;
delete object.#privateProperty;
1 sẽ không có bất kỳ ảnh hưởng nào và sẽ trả lại
delete identifier;
delete object.#privateProperty;
6. Trong chế độ nghiêm ngặt, điều này sẽ tăng
delete identifier;
delete object.#privateProperty;
7.

const Employee = {};
Object.defineProperty[Employee, "name", { configurable: false }];

console.log[delete Employee.name]; // returns false

// Creates the property empCount on the global scope.
// Since we are using var, this is marked as non-configurable.
var empCount = 43;

// Creates the property adminName on the global scope.
// Since it was defined without "var", it is marked configurable.
EmployeeDetails = {
  name: "xyz",
  age: 5,
  designation: "Developer",
};

// delete can be used to remove properties from objects.
delete EmployeeDetails.name; // returns true

// Even when the property does not exist, delete returns "true".
delete EmployeeDetails.salary; // returns true

// EmployeeDetails is a property of the global scope.
delete EmployeeDetails; // returns true

// On the contrary, empCount is not configurable
// since var was used.
delete empCount; // returns false

// delete also does not affect built-in static properties
// that are non-configurable.
delete Math.PI; // returns false

function f[] {
  var z = 44;

  // delete doesn't affect local variable names
  delete z; // returns false
}
9 Tạo các thuộc tính không thể cấu hình được không thể xóa với toán tử
delete identifier;
delete object.#privateProperty;
1:

// Since "nameOther" is added using with the
// var keyword, it is marked as non-configurable
var nameOther = "XYZ";

// We can access this global property using:
Object.getOwnPropertyDescriptor[globalThis, "nameOther"];
// Object {
//   value: "XYZ",
//   writable: true,
//   enumerable: true,
//   configurable: false
// }

delete globalThis.nameOther; // return false

Trong chế độ nghiêm ngặt, điều này sẽ tăng một ngoại lệ.

Xóa tài sản toàn cầu

Nếu một thuộc tính toàn cầu có thể định cấu hình [ví dụ, thông qua gán tài sản trực tiếp], nó có thể bị xóa và các tài liệu tham khảo tiếp theo cho chúng vì các biến toàn cầu sẽ tạo ra

delete identifier;
delete object.#privateProperty;
8.

delete identifier;
delete object.#privateProperty;
0

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-Delete-Coperator
# sec-delete-operator

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

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Làm thế nào để bạn xóa một khóa khỏi đối tượng JS?

Sử dụng Xóa để xóa các phím đối tượng Xóa từ khóa JavaScript đặc biệt được sử dụng để xóa các phím đối tượng [còn được gọi là thuộc tính đối tượng]. Mặc dù bạn có thể nghĩ rằng việc thiết lập một khóa đối tượng bằng với không xác định sẽ xóa nó, vì không xác định là giá trị mà các phím đối tượng chưa được đặt, khóa vẫn sẽ tồn tại. The special JavaScript keyword delete is used to remove object keys [also called object properties]. While you might think setting an object key equal to undefined would delete it, since undefined is the value that object keys that have not yet been set have, the key would still exist.

Làm cách nào để xóa một khóa cụ thể

Toán tử xóa được sử dụng để xóa cặp giá trị khóa trong đó khóa là Key Key2. Bảng điều khiển. log [obj]; Đầu ra của mã trên trong bảng điều khiển sẽ là: {key1: "value1", key3: "value3"}. where the key is “key2”. console. log[obj]; The output of the above code in the console will be: { key1: "value1", key3: "value3" }.

Làm cách nào để loại bỏ một khóa cụ thể khỏi một mảng các đối tượng?

Để xóa một thuộc tính khỏi tất cả các đối tượng trong một mảng: sử dụng phương thức mảng.foreach [] để lặp qua mảng. Mỗi lần lặp, hãy sử dụng toán tử xóa để xóa thuộc tính cụ thể.Thuộc tính sẽ được xóa khỏi tất cả các đối tượng trong mảng.Use the Array. forEach[] method to iterate over the array. On each iteration, use the delete operator to delete the specific property. The property will get removed from all objects in the array.

Làm cách nào để loại bỏ một thuộc tính khỏi một đối tượng?

Xóa thuộc tính khỏi một đối tượng, toán tử xóa xóa cả giá trị của thuộc tính và chính thuộc tính.Sau khi xóa, tài sản không thể được sử dụng trước khi được thêm lại.Toán tử xóa được thiết kế để sử dụng trên các thuộc tính đối tượng.Nó không có tác dụng đối với các biến hoặc chức năng.The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

Bài Viết Liên Quan

Chủ Đề