| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Working_with_objects | [Back] [Original] |
Get to know MDN better
JavaScript () JavaScript JavaScript
C++
const obj = {
property1: value1, // property_#
2: value2, //
"property n": value3, //
};
obj
cond true x
let x;
if (cond) {
x = { greeting: "hi there" };
}
3 myHonda engine
const myHonda = {
color: "red",
wheels: 4,
engine: { cylinders: 4, size: 2.2 },
};
2
new Car makemodelyear
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
this
myCar
const myCar = new Car("Eagle", "Talon TSi", 1993);
myCar myCar.make "Eagle" myCar.model "Talon TSi"myCar.year 1993
Car new
const randCar = new Car("Nissan", "300ZX", 1992);
const kenCar = new Car("Mazda", "Miata", 1990);
Person
function Person(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
2 Person
const rand = new Person("Rand McKinnon", 33, "M");
const ken = new Person("Ken Jones", 39, "M");
Car Person owner
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
const car1 = new Car("Eagle", "Talon TSi", 1993, rand);
const car2 = new Car("Nissan", "300ZX", 1992, ken);
rand ken owner car2 owner
car2.owner.name;
car1.color = "black";
car1 color "black" Car
// Animal
const animalProto = {
type: "Invertebrates", //
displayType() {
// Animal
console.log(this.type);
},
};
// animal animal
const animal = Object.create(animalProto);
animal.displayType(); // : Invertebrates
// Fishes animal
const fish = Object.create(animalProto);
fish.type = "Fishes";
fish.displayType(); // : Fishes
myCar makemodelyear "Ford""Mustang"1969
const myCar = {
make: "Ford",
model: "Mustang",
year: 1969,
};
//
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
//
myCar["make"] = "Ford";
myCar["model"] = "Mustang";
myCar["year"] = 1969;
const myObj = {};
const str = "myString";
const rand = Math.random();
const anotherObj = {};
// Create additional properties on myObj
myObj.type = "type ";
myObj["date created"] = "";
myObj[str] = " str ";
myObj[rand] = "";
myObj[anotherObj] = " anotherObj ";
myObj[""] = "";
console.log(myObj);
// {
// type: 'type ',
// 'date created': '',
// myString: ' str ',
// '0.6398914448618778': '',
// '[object Object]': ' anotherObj ',
// '': ''
// }
console.log(myObj.myString); // ' str '
anotherObj myObj JavaScript toString() anotherObj
str "myString" "myString" myObj.str undefined
str = "myString";
myObj[str] = " str ";
console.log(myObj.str); // undefined
console.log(myObj[str]); // ' str '
console.log(myObj.myString); // ' str '
let propertyName = "make";
myCar[propertyName] = "Ford";
//
propertyName = "model";
myCar[propertyName] = "Mustang";
console.log(myCar); // { make: 'Ford', model: 'Mustang' }
myCar.nonexistentProperty; // undefined
/ 3
for...in Object.keys() myObj Object.getOwnPropertyNames() myObj function showProps(obj, objName) {
let result = "";
for (const i in obj) {
// Object.hasOwn()
if (Object.hasOwn(obj, i)) {
result += `${objName}.${i} = ${obj[i]}\n`;
}
}
console.log(result);
}
showProps(myCar, 'myCar')
myCar.make = Ford myCar.model = Mustang myCar.year = 1969
function showProps(obj, objName) {
let result = "";
Object.keys(obj).forEach((i) => {
result += `${objName}.${i} = ${obj[i]}\n`;
});
console.log(result);
}
function listAllProperties(myObj) {
let objectToInspect = myObj;
let result = [];
while (objectToInspect !== null) {
result = result.concat(Object.getOwnPropertyNames(objectToInspect));
objectToInspect = Object.getPrototypeOf(objectToInspect);
}
return result;
}
// 2 a b myobj
const myObj = { a: 5, b: 12 };
// a myobj b
delete myObj.a;
console.log("a" in myObj); // false
prototype 1 color Car car1
Car.prototype.color = "red";
console.log(car1.color); // "red"
objectName.methodName = functionName;
const myObj = {
myMethod: function (params) {
// ...
},
//
myOtherMethod(params) {
// ...
},
};
objectName methodName functionName
objectName.methodName(params);
prototype Car
Car.prototype.displayCar = function () {
const result = ` ${this.year} ${this.make} ${this.model}`;
console.log(result);
};
this displayCar
car1.displayCar();
car2.displayCar();
JavaScript this manager intern 2 name, age, job sayHi() this.name 2
const manager = {
name: "Karina",
age: 27,
job: "Software Engineer",
};
const intern = {
name: "Tyrone",
age: 21,
job: "Software Engineer Intern",
};
function sayHi() {
console.log(`Hello, my name is ${this.name}`);
}
// sayHi
manager.sayHi = sayHi;
intern.sayHi = sayHi;
manager.sayHi(); // Hello, my name is Karina
intern.sayHi(); // Hello, my name is Tyrone
this manager.sayHi() this manager manager sayHi() this Function.prototype.call() Reflect.apply() this
const myObj = {
a: 7,
get b() {
return this.a + 1;
},
set c(x) {
this.a = x / 2;
},
};
console.log(myObj.a); // 7
console.log(myObj.b); // 8 get b()
myObj.c = 50; // set c(x)
console.log(myObj.a); // 25
myObj
myObj.a myObj.b myObj.a + 1 myObj.c myObj.c 1/2 myObj.a const myObj = { a: 0 };
Object.defineProperties(myObj, {
b: {
get() {
return this.a + 1;
},
},
c: {
set(x) {
this.a = x / 2;
},
},
});
myObj.c = 10; // 'a' 10 / 2 (5)
console.log(myObj.b); // a + 1 6
2 2 2 JavaScript
JavaScript 2
// 2 2
const fruit = { name: "apple" };
const anotherFruit = { name: "apple" };
fruit == anotherFruit; // false
fruit === anotherFruit; // false
// 2 1
const fruit = { name: "apple" };
const anotherFruit = fruit; // anotherFruit fruit
// fruit anotherFruit
fruit == anotherFruit; // true
fruit === anotherFruit; // true
fruit.name = "grape";
console.log(anotherFruit); // { name: "apple" } { name: "grape" }
| Web Proxy Viewer | New URL | Original Page |