<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript 对象相关方法总结</title>
</head>
<body>
<h1>JavaScript 对象相关方法总结</h1>
<script>
function Animal(name, color, arrs) {
this.name = name;
this.color = color || "yellow";
this.arrs = arrs;
}
Animal.prototype.run = function() {
console.log("Animal run");
};
Animal.prototype.multi = ["a", "b", "c"];
function Cat(name, color, arrs, foo) {
Animal.call(this, name, color, arrs);
this.name = name;
this.ids = [1, 2, 3, 4];
this.foo = foo;
}
Cat.prototype = new Animal();
Cat.prototype.fly = function() {
console.log("Cat fly");
};
Cat.prototype.progress = 88;
var cat = new Cat("皮特", "red", ["a", "啊", 1], "哈林");
console.log("cat", cat);
console.log("instanceof Animal", cat instanceof Animal);
console.log("instanceof Cat", cat instanceof Cat);
console.log("Animal.prototype.isPrototypeOf(cat)", Animal.prototype.isPrototypeOf(cat));
console.log("Cat.prototype.isPrototypeOf(cat)", Cat.prototype.isPrototypeOf(cat));
console.log("'fly' in cat", "fly" in cat);
console.log("'fly' in color", "color" in cat);
console.log("'fly' in run", "run" in cat);
console.log("'fly' in eat", "eat" in cat);
console.log("Cat.hasOwnProperty('progress')", Cat.hasOwnProperty("progress"));
console.log("Cat.hasOwnProperty('color')", Cat.hasOwnProperty("color"));
console.log("Cat.hasOwnProperty('name')", Cat.hasOwnProperty("name"));
console.log("Object.getPrototypeOf(cat)", Object.getPrototypeOf(cat), Object.getPrototypeOf(cat) === cat.__proto__, Object.getPrototypeOf(cat) === Cat.prototype);
console.log("Object.create(cat)", Object.create(cat));
console.log("Animal.prototype", Animal.prototype);
console.log("Cat.prototype", Cat.prototype);
console.log("cat.__proto__", cat.__proto__);
console.log("Cat.prototype === cat.__proto__", Cat.prototype === cat.__proto__);
console.log("Animal.prototype === cat.__proto__.__proto__", Animal.prototype === cat.__proto__.__proto__);
Object.defineProperty(cat, "name", {
writable: true,
value: "山姆",
configurable: true,
});
console.log(cat.name);
Object.defineProperty(cat, "name", {
enumerable: false,
configurable: true,
get: function getter() {
console.log("get value");
return this.value;
},
set: function setter(val) {
console.log("set value", val);
this.value = val;
}
});
cat.name = "托尼";
console.log(cat.name);
Object.defineProperties(cat, {
"name": {
enumerable: true,
}
});
console.log("getOwnPropertyDescriptors", Object.getOwnPropertyDescriptor(cat, "name"));
console.log("getOwnPropertyDescriptors", Object.getOwnPropertyDescriptors(cat));
for (var key in cat) {
console.log("key", key);
}
console.log("Object.keys", Object.keys(cat));
console.log("Object.keys", Object.values(cat));
for (let [key, value] of Object.entries(cat)) {
console.log("entries", key, value);
}
var fromEntries = Object.fromEntries([
["name", "bar"],
["foo", 100]
]);
console.log("fromEntries", fromEntries);
var map = new Map([
["name", "bar"],
["foo", 100]
]);
console.log(map);
console.log("fromEntries map", Object.fromEntries(map));
var obj1 = {
name: "hello world"
};
var obj2 = {
name: "hello world"
};
console.log("Object.is", Object.is(obj1, obj2));
var obj3 = {
name: "obj 3",
};
var obj4 = {
name: "obj 4",
color: "blue",
arrs: [1, 2, 3]
};
Object.assign(obj3, obj4);
obj4.color = "pink";
obj4.arrs[0] = 10;
console.log("Object.assign", obj3, obj4);
</script>
</body>
</html>