[] trong javascript là gì?

Một trong những chủ đề đầu tiên bạn sẽ gặp khi học JavaScript (hoặc bất kỳ ngôn ngữ lập trình nào khác) là toán tử

Các toán tử phổ biến nhất là toán tử số học, logic và so sánh. Nhưng bạn có biết rằng JavaScript có toán tử in không?

Nếu bạn không, đừng băn khoăn. Tôi mới bắt gặp nó gần đây khi đang tìm kiếm giải pháp cho một vấn đề trên Google

Trong bài viết này, bạn sẽ tìm hiểu chính xác chức năng của toán tử in trong JavaScript, khi nào nên sử dụng và cách sử dụng

Chính xác thì JavaScript trong toán tử là gì?

Toán tử JavaScript in được sử dụng để kiểm tra xem một thuộc tính được chỉ định có tồn tại trong một đối tượng hoặc trong các thuộc tính kế thừa của nó hay không (nói cách khác, chuỗi nguyên mẫu của nó). Toán tử in trả về

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
1 nếu thuộc tính được chỉ định tồn tại

[] trong javascript là gì?
Cấu trúc của một đối tượng JavaScript đơn giản

Chuỗi nguyên mẫu JavaScript là cách các đối tượng hoặc phiên bản đối tượng có quyền truy cập vào các thuộc tính và phương thức ban đầu không phải của chúng. Các đối tượng này kế thừa các thuộc tính và phương thức được xác định trong hàm tạo hoặc nguyên mẫu của chúng, có thể được truy cập thông qua thuộc tính

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
2 của chúng

Bài viết này giả định rằng bạn đã có hiểu biết cơ bản về đối tượng là gì, cách tạo chúng, mục đích sử dụng của chúng và cách hoạt động của tính kế thừa JavaScript. Nếu bạn không, nên giúp đỡ

Khi nào nên sử dụng JavaScript trong toán tử

Để xác minh xem một thuộc tính có tồn tại trên một đối tượng hay không

const car = {
  make: 'Toyota',
  model:'Camry',
  year: '2018',
  start: function() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

'make' in car // Returns true.
'start' in car // Returns true.
'Toyota' in car // Returns false. 'Toyota' is not a property name, but a value.

Để xác minh xem một thuộc tính có được kế thừa bởi một đối tượng hay không

Hãy sử dụng cú pháp lớp ES6 để tạo một hàm tạo đối tượng. Điều này cũng sẽ áp dụng cho hàm tạo chức năng

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */

Để xác minh xem chỉ mục/khóa có tồn tại trên một mảng không

Bạn có thể thắc mắc, vì chúng ta đã xác định rằng toán tử in trong JavaScript có thể được sử dụng với các đối tượng, vậy tại sao chúng ta cũng có thể sử dụng nó với các mảng?

Chà, một mảng thực sự là một nguyên mẫu (ví dụ) của loại

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4. Trên thực tế, mọi thứ trong JavaScript đều là một thể hiện của loại
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4

Điều đó nghe có vẻ điên rồ, nhưng hãy chạy một chương trình đơn giản trong bảng điều khiển của trình duyệt để xác nhận

Đầu tiên, xác định một mảng và xác nhận xem nó có phải là một thể hiện của loại

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4 hay không bằng cách sử dụng toán tử
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
7

________số 8_______

Vẫn còn nghi ngờ?

Bạn sẽ nhận thấy một danh sách các thuộc tính, một trong số đó là

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
2 trỏ đến
const number = [2, 3, 4, 5];

number instanceof Object // Returns true
0. Mở nó ra và đi xuống danh sách đó đưa chúng ta đến một tài sản
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
2 khác với giá trị là
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4

Điều đó cho thấy rằng mảng

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
8 là một thể hiện của kiểu
const number = [2, 3, 4, 5];

number instanceof Object // Returns true
0 là một thể hiện của kiểu
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4

Bây giờ, quay lại sử dụng toán tử in

const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/

Để xác minh xem một thuộc tính có tồn tại trên phần tử Html hay không

Trong bài viết của Kirupa, Kiểm tra xem bạn có đang sử dụng thiết bị hỗ trợ cảm ứng hay không, anh ấy đã nêu bật chức năng này

function isTouchSupported() {
  var msTouchEnabled = window.navigator.msMaxTouchPoints;
  var generalTouchEnabled = "ontouchstart" in document.createElement("div");

  if (msTouchEnabled || generalTouchEnabled) {
    return true;
  }
  return false;
}

Hàm này trả về

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
1 nếu bạn đang sử dụng thiết bị hỗ trợ cảm ứng và trả về
const number = [2, 3, 4, 5];

number instanceof Object // Returns true
8 nếu bạn đang sử dụng thiết bị không hỗ trợ cảm ứng bằng cách kiểm tra xem các thuộc tính
const number = [2, 3, 4, 5];

number instanceof Object // Returns true
9 và
const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
0 có hiện diện hay không. Các thuộc tính này chỉ tồn tại trên các thiết bị hỗ trợ cảm ứng

khá đơn giản

Hãy tập trung vào dòng được đánh dấu. Hãy nhớ cách chúng tôi thiết lập rằng toán tử in trả về

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
1 nếu thuộc tính được chỉ định tồn tại trong một đối tượng?

Tất nhiên, bạn có thể không tin tôi nếu không có bằng chứng nào đó. Như trước đây, hãy nhập một số lệnh vào bảng điều khiển

Tạo một phần tử

const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
4 và liệt kê các thuộc tính của nó bằng cách sử dụng
const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
5

const element = document.createElement('div');

console.dir(element);

Sau đó, bạn sẽ thấy phần tử

const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
4 với các thuộc tính được liệt kê trong bảng điều khiển

Mở trình đơn thả xuống và bạn sẽ nhận thấy rằng nó có thuộc tính

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
2 của
const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
8. Mở nó ra và bạn sẽ tìm thấy một thuộc tính
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
2 khác của
function isTouchSupported() {
  var msTouchEnabled = window.navigator.msMaxTouchPoints;
  var generalTouchEnabled = "ontouchstart" in document.createElement("div");

  if (msTouchEnabled || generalTouchEnabled) {
    return true;
  }
  return false;
}
0, sau đó là
function isTouchSupported() {
  var msTouchEnabled = window.navigator.msMaxTouchPoints;
  var generalTouchEnabled = "ontouchstart" in document.createElement("div");

  if (msTouchEnabled || generalTouchEnabled) {
    return true;
  }
  return false;
}
1,
function isTouchSupported() {
  var msTouchEnabled = window.navigator.msMaxTouchPoints;
  var generalTouchEnabled = "ontouchstart" in document.createElement("div");

  if (msTouchEnabled || generalTouchEnabled) {
    return true;
  }
  return false;
}
2,
function isTouchSupported() {
  var msTouchEnabled = window.navigator.msMaxTouchPoints;
  var generalTouchEnabled = "ontouchstart" in document.createElement("div");

  if (msTouchEnabled || generalTouchEnabled) {
    return true;
  }
  return false;
}
3 và cuối cùng là
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4

Cũng chạy

element instanceof Object

Điều này sẽ trả về

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
1, cho thấy rằng phần tử
const number = [2, 3, 4, 5];

3 in number // Returns true.
2 in number // Returns true.

5 in number // Returns false because 5 is not an existing index on the array but a value;

'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/
4 là một thể hiện của loại
class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4, đó là lý do tại sao toán tử in có thể được sử dụng trên nó

Phần kết luận

Bạn đã tìm hiểu về toán tử in không phổ biến trong JavaScript, được sử dụng để xác minh sự hiện diện của các thuộc tính trên một đối tượng hoặc các thể hiện loại

class Car {
  constructor(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }
  start() {
    console.log(`Starting ${this.make} ${this.model}, ${this.year}`);
  }
}

const toyota = new Car('Toyota', 'Camry', '2018');

'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */

'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */
4. Điều này sẽ có ích khi viết logic xác minh

Nếu bạn thích bài viết này, chắc chắn bạn sẽ thích các bài viết khác trên blog của tôi codewithlinda. com. Ở đó tôi xuất bản các bài viết thân thiện với người mới bắt đầu về phát triển giao diện người dùng không có biệt ngữ kỹ thuật (càng nhiều càng tốt) ?

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO


[] trong javascript là gì?
Linda Ikechukwu

Người ủng hộ nhà phát triển tại Smallstep Labs. Ở đây để tạo PKI và chứng chỉ kỹ thuật số dễ dàng và thú vị


Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

[[[ ]]] này trong JavaScript là gì?

Đó là viết tắt của mảng trống . Tương tự như mảng mới(). Ngoài ra {} là một đối tượng trống. Các đối tượng giống như bảng băm trong Js để bạn có thể sử dụng nó làm từ điển.

Sự khác biệt giữa [] và {} là gì?

Sự khác biệt giữa “{}” và “[]” là {} là một mảng trống trong khi [] là một mảng JavaScript . Trong JavaScript, hầu hết “mọi thứ” đều là một đối tượng. Tất cả các giá trị JavaScript, ngoại trừ các giá trị nguyên thủy, là các đối tượng.

Tại sao [] == [] sai trong JS?

Bởi vì [] tạo một mảng mới, nên bạn đang so sánh một đối tượng mảng với một đối tượng mảng khác. Nó không phải là nội dung của các mảng được so sánh, các tham chiếu đối tượng được so sánh. Chúng không bằng nhau vì không phải là đối tượng giống nhau .

Sự khác biệt giữa {} và [] trong một mảng là gì?

Không có gì khác biệt khi bạn khởi tạo mảng không có độ dài bất kỳ . Vì vậy, var a = [] & var b = new Array() giống nhau. Nhưng nếu bạn khởi tạo mảng với độ dài như thế nào var b = new Array(1); .