| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/in | [Back] [Original] |
Get to know MDN better
in true
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"
prop in object
#prop in object
TypeErrorobject
undefined "x" in obj obj.x !== undefined in false undefined delete
in
undefined TypeError
class C {
#x;
static isC(obj) {
try {
obj.#x;
return true;
} catch {
return false;
}
}
}
class C {
#x;
static isC(obj) {
return #x in obj;
}
}
in SyntaxError ("Private field '#x' must be declared in an enclosing class")
class C {
foo() {
#x in this;
}
}
new C().foo(); // SyntaxError: Private field '#x' must be declared in an enclosing class
in
//
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; // true (length Array )
Symbol.iterator in trees; // true ( ES2015 )
//
"PI" in Math; // true
//
const myCar = { make: "Honda", model: "Accord", year: 1998 };
"make" in myCar; // true
"model" in myCar; // true
in String
const color1 = new String("green");
"length" in color1; // true
const color2 = "coral";
// (color2 String )
"length" in color2;
in delete in false
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
const myCar = { make: "Honda", model: "Accord", year: 1998 };
myCar.make = undefined;
"make" in myCar; // true
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees[3] = undefined;
3 in trees; // true
in false undefined
const empties = new Array(3);
empties[2]; // undefined
2 in empties; // false
const empties = new Array(3).fill(undefined);
2 in empties; // true
in true
const ages = { alice: 18, bob: 27 };
function hasPerson(name) {
return name in ages;
}
hasPerson("hasOwnProperty"); // true
const ages = { alice: 18, bob: 27 };
function hasPerson(name) {
return Object.hasOwn(ages, name);
}
hasPerson("hasOwnProperty"); // false
const ages = new Map([
["alice", 18],
["bob", 27],
]);
function hasPerson(name) {
return ages.has(name);
}
hasPerson("hasOwnProperty"); // false
Person
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
}
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
class Person {
#age;
constructor(age) {
this.#age = age;
}
static [Symbol.hasInstance](o) {
// Testing `this` to prevent false-positives when
// calling `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 |