**
快速获取属性的方法,在日常的 接触中 大概了解了 以下几种 :
**
1.使用 Object.keys方法
const a = {
name: "ls",
age: 18,
score: 99,
getProperty: function() {
return this.age
}
console.log(Object.keys(a))
// ["name", "age", "score", "getProperty"]
2.使用 object.hasOwnpropertyName()
const b = {
name: "ls",
age: 18,
score: 99,
getProperty: function() {
return this.age
}
console.log(Object.hasOwnpropertyName(b))
3.使用 Reflect.ownKeys()
const C = {
name: "ls",
age: 18,
score: 99,
getProperty: function() {
return this.age
}
console.log(Reflect.ownKeys(b))
- for in循环
const a = {
name: "ls",
age: 18,
score: 99,
getProperty: function() {
return this.age
}
const property = [];
for (const key in a) {
if (a.hasOwnProperty(key)) {
property.push(key);
}
}
console.log(property);
//["name", "age", "score", "getProperty"]
版权声明:本文为weixin_46642209原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。