Hướng dẫn javascript pop from object - javascript bật từ đối tượng

Phương thức pop() loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó. Phương pháp này thay đổi độ dài của mảng.pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Thử nó

Cú pháp

Giá trị trả về

Phần tử bị loại bỏ khỏi mảng; undefined Nếu mảng trống.

Sự mô tả

Phương thức pop() loại bỏ phần tử cuối cùng khỏi một mảng và trả về giá trị đó cho người gọi. Nếu bạn gọi pop() trên một mảng trống, nó sẽ trả về undefined.

Array.prototype.shift() có hành vi tương tự như pop(), nhưng áp dụng cho phần tử đầu tiên trong một mảng.

Phương pháp pop() là một phương pháp đột biến. Nó thay đổi độ dài và nội dung của

const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
1. Trong trường hợp bạn muốn giá trị của
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
1 giống nhau, nhưng trả về một mảng mới với phần tử cuối cùng bị xóa, bạn có thể sử dụng
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
3 thay thế.

Phương pháp pop() là chung chung. Nó chỉ mong đợi giá trị

const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
1 có thuộc tính
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
6 và các thuộc tính được khóa. Mặc dù các chuỗi cũng giống như mảng, phương pháp này không phù hợp để được áp dụng trên chúng, vì các chuỗi là bất biến.

Ví dụ

Loại bỏ phần tử cuối cùng của một mảng

Mã sau đây tạo ra mảng

const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
7 chứa bốn phần tử, sau đó loại bỏ phần tử cuối cùng của nó.

const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];

const popped = myFish.pop();

console.log(myFish); // ['angel', 'clown', 'mandarin' ]

console.log(popped); // 'sturgeon'

Gọi pop () trên các đối tượng không phải là

Phương pháp pop() đọc thuộc tính

const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
6 của
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
1. Nếu độ dài được chuẩn hóa là 0,
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }
6 được đặt thành
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
2 một lần nữa (trong khi nó có thể là âm hoặc undefined trước đó). Nếu không, tài sản tại
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
4 được trả lại và xóa.

const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.pop.call(arrayLike));
// 4
console.log(arrayLike);
// { length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.pop.call(plainObj);
console.log(plainObj);
// { length: 0 }

Sử dụng một đối tượng theo kiểu giống như mảng

const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
5 và
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
6 có chủ ý chung chung và chúng ta có thể sử dụng điều đó để làm lợi thế của mình - như ví dụ sau đây cho thấy.

Lưu ý rằng trong ví dụ này, chúng tôi không tạo một mảng để lưu trữ một bộ sưu tập các đối tượng. Thay vào đó, chúng tôi lưu trữ bộ sưu tập trên chính đối tượng và sử dụng

const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
7 trên
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
8 và
const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2
9 để lừa các phương pháp đó nghĩ rằng chúng tôi đang xử lý một mảng.

const collection = {
  length: 0,
  addElements(...elements) {
    // obj.length will be incremented automatically
    // every time an element is added.

    // Returning what push returns; that is
    // the new value of length property.
    return [].push.call(this, ...elements);
  },
  removeElement() {
    // obj.length will be decremented automatically
    // every time an element is removed.

    // Returning what pop returns; that is
    // the removed element.
    return [].pop.call(this);
  }
}

collection.addElements(10, 20, 30);
console.log(collection.length);  // 3
collection.removeElement();
console.log(collection.length);  // 2

Thông số kỹ thuật

Sự chỉ rõ
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-array.prototype.pop
# sec-array.prototype.pop

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