[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/in [Back]  [Original]

in - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

in

20157

in true

in Array.prototype.includes() Set.prototype.has()

const car = { make: "Honda", model: "Accord", year: 1998 };

console.log("make" in car);
// true

delete car.make;
if ("make" in car === false) {
  car.make = "Suzuki";
}

console.log(car.make);
// "Suzuki"

js
prop in object
#prop in object

prop

symbol symbol

object

prop

TypeError

object

in symbol Object.hasOwn()

undefined"x" in obj obj.x !== undefined in false delete undefined

in true falsebranded check true

in

TypeError undefined

js
class C {
  #x;
  static isC(obj) {
    try {
      obj.#x;
      return true;
    } catch {
      return false;
    }
  }
}

js
class C {
  #x;
  static isC(obj) {
    return #x in obj;
  }
}

in SyntaxErrorPrivate field '#x' must be declared in an enclosing class

js
class C {
  foo() {
    #x in this;
  }
}

new C().foo(); // SyntaxError: Private field '#x' must be declared in an enclosing class

in

js
// 
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
0 in trees; //  true
3 in trees; //  true
6 in trees; //  false
"bay" in trees; //  false
"length" in trees; //  truelength 
Symbol.iterator in trees; //  true

// 
"PI" in Math; //  true

// 
const myCar = { make: "Honda", model: "Accord", year: 1998 };
"make" in myCar; //  true
"model" in myCar; //  true

in String

js
const color1 = new String("green");
"length" in color1; //  true

const color2 = "coral";
// color2  String 
"length" in color2;

undefined in

delete in false

js
const myCar = { make: "Honda", model: "Accord", year: 1998 };
delete myCar.make;
"make" in myCar; //  false

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

undefined in true

js
const myCar = { make: "Honda", model: "Accord", year: 1998 };
myCar.make = undefined;
"make" in myCar; //  true
js
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees[3] = undefined;
3 in trees; //  true

undefinedin false

js
const empties = new Array(3);
empties[2]; //  undefined
2 in empties; //  false

js
const empties = new Array(3).fill(undefined);
2 in empties; //  true

in true

js
const ages = { alice: 18, bob: 27 };

function hasPerson(name) {
  return name in ages;
}

hasPerson("hasOwnProperty"); //  true

Object.hasOwn()

js
const ages = { alice: 18, bob: 27 };

function hasPerson(name) {
  return Object.hasOwn(ages, name);
}

hasPerson("hasOwnProperty"); //  false

null Map ages

js
const ages = new Map([
  ["alice", 18],
  ["bob", 27],
]);

function hasPerson(name) {
  return ages.has(name);
}

hasPerson("hasOwnProperty"); //  false

in

Person

js
class Person {
  #age;
  constructor(age) {
    this.#age = age;
  }
  static isPerson(o) {
    return #age in o;
  }
  ageDifference(other) {
    return this.#age - other.#age;
  }
}

const p1 = new Person(20);
const p2 = new Person(30);
console.log(p1.ageDifference(p2)); // -10
console.log(Person.isPerson(p1)); //  true

if (Person.isPerson(p1) && Person.isPerson(p2)) {
  console.log(p1.ageDifference(p2)); // -10
}

js
const p2 = {};

p1.ageDifference(p2); // TypeError: Cannot read private member #age from an object whose class did not declare it

in try...catch

[Symbol.hasInstance]() instanceof Person.prototype

js
class Person {
  #age;
  constructor(age) {
    this.#age = age;
  }
  static [Symbol.hasInstance](o) {
    //  `this` `instanceof SubclassOfPerson` 
    return this === Person && #age in o;
  }
  ageDifference(other) {
    return this.#age - other.#age;
  }
}

const p1 = new Person(20);
const p2 = new Person(30);

if (p1 instanceof Person && p2 instanceof Person) {
  console.log(p1.ageDifference(p2)); // -10
}

ECMAScript 2027 LanguageSpecification
# sec-relational-operators


Web Proxy Viewer  |  New URL  |  Original Page