function Person(name, age) {
this.name = name;
this.age = age;
}
const person = new Person();
console.log(person.__proto__); // {constructor: f Person(){}}
原因:
let proto = {
x: 20,
y: 30
}
let obj = {
z: 40
}
let obj1 = Object.setPrototypeOf(obj, proto);
console.log(obj);
/**
* {
* z: 40,
* [[prototype]]: Object
* x: 20,
* y: 30
* }
*/
console.log(obj === obj1); // true
// 1 -> 经过Number包装类 -> 打印的是Number上的原型,默认是0
let obj = Object.setPrototypeOf(1, {a: 1});
console.log(obj); // 1
console.log(Object.getPrototypeOf(obj)); // Number{0}
console.log(Object.getPrototypeOf(Number(1)) === Number.prototype); // true
同理,string跟boolean也会失效:
let obj = Object.setPrototypeOf('1', {a: 1});
console.log(obj); // '1'
console.log(Object.getPrototypeOf(obj)); // String{''}
let obj2 = Object.setPrototypeOf(true, {a: 1});
console.log(obj2); // true
console.log(Object.getPrototypeOf(obj)); // Boolean{false}
let obj = Object.setPrototypeOf(undefined, {a: 1}); // 报错
let obj2 = Object.setPrototypeOf(null, {a: 1}); // 报错
Object.keys()
Object.values()
const foo = {
a: 1,
b: 2,
c: 3
}
Object.defineProperties(foo, {
d: {
value: 4,
enumerable: true
},
e: {
value: 5,
enumerable: false
}
});
// 返回对象可枚举的属性的键名
console.log(Object.keys(foo)); // (4) ['a', 'b', 'c', 'd']
// 返回对象可枚举的属性的键值
console.log(Object.values(foo)); // (4) [1, 2, 3, 4]
console.log(Object.entries(foo));
// [
// [
// "a",
// 1
// ],
// [
// "b",
// 2
// ],
// [
// "c",
// 3
// ],
// [
// "d",
// 4
// ]
// ]
let obj = {};
Object.keys(obj); // []
let obj = 1;
Object.keys(obj); // []
let obj = true;
Object.keys(obj); // []
let obj = '123';
Object.keys(obj); // ['0', '1', '2']
// values()跟entries()方法同理
只有在对象方法的简写中才能生效⭐
let proto = {
x: 20,
y: 30,
bar: function() {
return this.y;
}
}
let obj = {
z: 40,
foo() {
return super.bar();
}
}
Object.setPrototypeOf(obj, proto);
console.log(obj.foo()); // 30
为了解决对象属性的重名问题
let s1 = Symbol;
console.log(s1); // ƒ Symbol() { [native code] }
let s1 = Symbol(),
s2 = Symbol();
console.log(s1, s1); // Symbol() Symbol()
console.log(s1 === s2); // false
console.log(typeof s1); // symbol
s1.a = 1;
console.log(s1.a); // undefined
const obj = {
a: 1
}
let a1 = Symbol(obj);
console.log(a1); // Symbol([object Object])
console.log(Symbol(undefined)); // Symbol()
// 通过Object.prototype.toString()转换
console.log(Symbol(null)); // Symbol(null)
// console.log(Symbol() + 1); // 报错
// console.log(Symbol() + '1'); // 报错
// Symbol只有Number不能转,String跟Boolean是可以的
// console.log(Number(a1)); // 报错
// Symbol不可以通过隐式转换,只能通过显示转换
// console.log(a1 + ''); // 报错
console.log(String(a1)); // Symbol([object Object])
console.log(Boolean(a1)); // true
console.log(!a1); // false
console.log(Object.getPrototypeOf(a1));
let obj = null;
let s1 = Symbol(obj);
console.log(s1.toString()); // 'Symbol(null)'
let name = Symbol();
let person = {};
// 使用defineProperty来定义
Object.defineProperty(person, name, {
value: 'lyb'
});
console.log(person.name); // undefined
console.log(person['name']); // undefined
console.log(person[name]); // lyb
let name = Symbol(),
eat = Symbol('eat');
let person = {
[name]: name,
[eat]() {
console.log(this[name]);
}
};
console.log(person); // {Symbol(): Symbol(), Symbol(eat): ƒ}
Symbol()声明的值永远不相等,for方法就是用来声明相等的Symbol类型值
● Symbol.for(key)
● 通过for里的key值来声明相等的Symbol类型值
let s = Symbol('foo');
let s1 = Symbol.for('foo'),
s2 = Symbol.for('foo');
console.log(s === s1); // false
console.log(s1 === s2); // true
获取Symbol.for()声明的key值
● Symbol.keyFor(key)
● 返回Symbol类型值的key
let s = Symbol('foo');
let s1 = Symbol.for('foo'),
s2 = Symbol.for('foo');
console.log(Symbol.keyFor(s1)); // foo
console.log(Symbol.keyFor(s2)); // foo
console.log(Symbol.keyFor(s1) === Symbol.keyFor(s1)); // true
console.log(Symbol.keyFor(s)); // undefined
● for in不可遍历Symbol属性的对象
● for of不可遍历Symbol属性的对象
● 获取对象中的Symbol类型的属性
● 参数:目标对象
● 返回值:Symbol类型的属性组成的数组
let s1 = Symbol('s1'),
s2 = Symbol('s2');
const obj = {};
obj[s1] = 's1Symbol';
obj[s2] = 's2Symbol';
obj.s3 = 's3Str';
console.log(obj); // {s3: 's3Str', Symbol(s1): 's1Symbol', Symbol(s2): 's2Symbol'}
const objSym = Object.getOwnPropertySymbols(obj);
console.log(objSym); // [Symbol(s1), Symbol(s2)]
● for in:遍历自身和可继承的可枚举的属性(不包含Symbol类型的)
● Object.keys():遍历自身的可枚举的属性(不包含Symbol类型的)
● Object.getOwnPropertySymbols():遍历自身的Symbol类型的值
● Object.assign():浅拷贝自身可枚举的包含Symbol类型的值
● JSON.stringify():遍历自身的可枚举的属性