原生js判断对象是否存在该属性以及存在的问题

本文探讨了JavaScript中几种判断对象属性存在性的方法,包括typeof、属性值检查、in操作符、hasOwnProperty以及Reflect.has,强调了它们在不同情况下的适用性和局限性。
摘要由CSDN通过智能技术生成

typeof

一般情况下,使用typeof操作符可以判断一个对象上是否存在该属性,我们看一下下面的代码

const obj = {
  name: "ray",
  job: "frontend",
  age: undefined,
};
console.log(typeof obj.name !== "undefined"); 
// 输出 true
console.log(typeof obj.job !== "undefined"); 
// 输出 true
console.log(typeof obj.age !== "undefined"); 
// 输出 false
console.log(typeof obj.hobby !== "undefined"); 
// 输出 false

通使用typeof可以判断一个对象上是否存在该属性,但是如果一个对象上有该属性且该属性的值为undefined时,那么此时判断会有问题。比如上面的代码:age属性的值为undefined,使用typeof判断的结果并不是我们期望的

undefined

一般情况下,我们利用属性值是否为undefined,可以判断一个对象上是否存在该属性,我们看一下下面的代码

const obj = {
  name: "ray",
  job: "frontend",
  age: undefined,
};
console.log(obj.name !== undefined); 
// 输出 true
console.log(obj.job !== undefined); 
// 输出 true
console.log(obj.age !== undefined); 
// 输出 false
console.log(obj.sax !== undefined); 
// 输出 false

使用属性值是否为undefined可以判断一个对象上是否存在该属性。但是如果一个对象上有该属性且该属性的值为undefined时,那么此时判断会有问题。比如上面的代码:age属性的值为undefined,使用属性值是否为undefined判断的结果并不是我们期望的。

null

一般情况下,我们利用属性值是否==null可以判断一个对象上是否存在该属性,这是因为在javascript中,null == undefined会返回true,我们看一下下面的代码

const obj = {
  name: "ray",
  job: "frontend",
  age: undefined,
  hobby: null,
  sex: "undefined",
};
console.log(obj.name != null); 
// 输出 true
console.log(obj.job != null); 
// 输出 true
console.log(obj.age != null); 
// 输出 false
console.log(obj.hobby != null); 
// 输出 false
console.log(obj.sax != null); 
// 输出 false
console.log(obj.address != null); 
// 输出 false

在不在乎类型的前提下,可以使用属性值是否为null来判断一个对象上是否存在该属性。但是一个对象上有该属性且该属性的值为undefined"undefined"null时会存在问题。比如上面的代码:age: undefinedsex: "undefined"hobby: null使用是否为null来判断均不是我们期望的

in

使用in操作符可以判断一个对象上是否存在该属性,我们看一下下面的代码

const obj = {
  name: "ray",
  job: "frontend",
};
console.log("name" in obj); 
// 输出 true
console.log("job" in obj); 
// 输出 true
console.log("age" in obj); 
// 输出 false

一般情况下,我们使用in可以判断一个对象上是否存在该属性。在js中,一切皆对象。操作符in无法判断出该属性是自己的还是继承来的,我们看一下以下代码

function Cat(name) {
  this.name = name;
}
Cat.prototype.type = "动物";
var cat = new Cat("猫");
console.log("name" in cat); 
// 输出 true
console.log("color" in cat); 
// 输出 false
console.log("type" in cat); 
// 输出 true
// ---------------------------------
class Cat1 {
  constructor(name) {
    this.name = name;
  }
}
Cat1.prototype.type = "动物";
var cat1 = new Cat1("狗");
console.log("name" in cat1); 
// 输出 true
console.log("color" in cat1); 
// 输出 false
console.log("type" in cat1); 
// 输出 true

在上面的代码中,如果我们仅判断catcat1上面是否存在某个属性时,上面的代码是没有问题的。但是如果我们需要判断该属性是否是自己的而非继承的,那么使用in操作符判断的结果并不是我们期望的

hasOwnProperty

大多数情况下,使用hasOwnProperty是判断一个对象是否存在该属性的首选方法。我们看一下以下代码

const obj = {
  name: "ray",
};
console.log(Object.prototype.hasOwnProperty.call(obj, "name")); 
// 输出 true
console.log(Object.prototype.hasOwnProperty.call(obj, "job")); 
// 输出 false

function Cat(name) {
  this.name = name;
}
Cat.prototype.type = "动物";
var cat = new Cat("大明");
console.log(Object.prototype.hasOwnProperty.call(cat, "name")); 
// 输出 true
console.log(Object.prototype.hasOwnProperty.call(cat, "type")); 
// 输出 false

class Cat1 {
  constructor(name) {
    this.name = name;
  }
}
Cat1.prototype.type = "动物";
var cat1 = new Cat1("大明", "黄色");
console.log(Object.prototype.hasOwnProperty.call(cat1, "name")); 
// 输出 true
console.log(Object.prototype.hasOwnProperty.call(cat1, "type")); 
// 输出 false

在上面的代码中,我们可以看出hasOwnProperty不仅能精确判断出一个对象是否存在该属性,且继承的属性也会返回false。换句话说,使用该方法可以判断出对象的属性是自己的还是继承来的

Reflect.has(target, key)

Reflect是ES6为了操作对象而提供的新API,而使用Reflect.has也可以判断一个对象是否存在该属性。我们看一下以下代码

const obj = {
  name: "ray",
};
console.log(Reflect.has(obj, "name")); 
// 输出 true
console.log(Reflect.has(obj, "job")); 
// 输出 false

function Cat(name) {
  this.name = name;
}
Cat.prototype.type = "动物";
var cat = new Cat("大明");
console.log(Reflect.has(cat, "name")); 
// 输出 true
console.log(Reflect.has(cat, "type")); 
// 输出 true

class Cat1 {
  constructor(name) {
    this.name = name;
  }
}
Cat1.prototype.type = "动物";
var cat1 = new Cat1("大明", "黄色");
console.log(Reflect.has(cat1, "name")); 
// 输出 true
console.log(Reflect.has(cat1, "type")); 
// 输出 true

关于更多Reflect的用法请查看

  • 17
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原生JS实现卡牌翻牌效果可以通过以下步骤实现: 1. 创建一个Card对象,包含卡片的图像路径、状态(是否翻开)等属性。 2. 创建一个Card数组,用于存储所有的卡片对象。 3. 使用Array.sort()方法对卡片数组进行乱序处理,以实现随机分配卡片。 4. 将乱序后的卡片数组分配到不同的方格下,并隐藏卡片的图像。 5. 监听鼠标点击事件,并通过事件委托判断点击的单位是否为已经翻开的卡片。 6. 根据点击的卡片的id判断是否与上一张翻开的卡片的id相同,从而判断是否翻牌成功。 7. 如果翻牌成功,则保持卡片翻开状态;如果翻牌不成功,则在0.3秒后自动翻转回方格。 8. 当所有卡片都成功翻牌时,游戏胜利。 以上是实现卡牌翻牌效果的主要逻辑。你可以根据需要自行编写代码实现。\[3\] #### 引用[.reference_title] - *1* *2* [cocos2d-x 卡牌翻牌效果的实现](https://blog.csdn.net/yanghuiliu/article/details/9115833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [原生JS记忆翻牌小游戏](https://blog.csdn.net/Y_qilin_/article/details/114292713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值