首页 > 其他分享 >【JS基础】hasOwnProperty 和 isPrototypeOf

【JS基础】hasOwnProperty 和 isPrototypeOf

时间:2023-06-23 21:33:23浏览次数:39  
标签:console log parent isPrototypeOf JS hasOwnProperty child

hasOwnProperty 检查对象是否含有某个属性,无法检查其原型链上是否含有该属性

isPrototypeOf 检查一个对象是否存在于另一个对象的原型链上,比如parent.isPrototypeof(child)检查 parent 对象是否在 child 对象的原型链上

 

console.log('--------------------  hasOwnProperty  -------------------');
// hasOwnProperty: check if the property is in the object
let obj = {
    name: 'obj',
    age: 18
}
console.log(obj.hasOwnProperty('name'));//true
console.log(obj.hasOwnProperty('toString'));//false


console.log('--------------------  isPrototypeOf  -------------------');
// isPrototypeOf: check if the object is in the prototype chain

// Creating a parent object
const parent = {
    name: 'Parent Object'
};

// Creating a child object that inherits from the parent object
const child = Object.create(parent);
child.age = 10;

// Checking if parent is in the prototype chain of child
console.log(parent.isPrototypeOf(child)); // Output: true

// Creating another object
const otherObj = {
    color: 'red'
};

// Checking if parent is in the prototype chain of otherObj
console.log(parent.isPrototypeOf(otherObj)); // Output: false

 

标签:console,log,parent,isPrototypeOf,JS,hasOwnProperty,child
From: https://www.cnblogs.com/zjy4fun/p/17500217.html

相关文章

  • 【js学习笔记二】innerHTML和innerText的使用
     目录前言导语代码部分 运行结果总结前言   我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语......
  • 【js学习笔记三】数组去重的第二种方式indexof
     目录前言导语代码部分 运行结果总结前言   我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从头再来歌谣的意志是永恒的放弃很容易但是坚持一定很酷导语......
  • JS(运算符、流程控制)
    一运算符(操作符)1运算符的分类运算符(operator)也被称为操作符,是用于实现赋值、比较和执行算数运算等功能的符号。JavaScript中常用的运算符有:算数运算符递增和递减运算符比较运算符逻辑运算符赋值运算符2算数运算符算术运算符概述概念:算术运算使用的符号,用于执......
  • Vue.js 过渡和动画
    学习目录:Vue.js简介Vue.js实例与数据绑定Vue.js计算属性和侦听器Vue.js条件渲染和列表渲染Vue.js事件处理Vue.js表单输入绑定Vue.js组件基础Vue.js组件通信Vue.js插槽Vue.js动态组件和异步组件Vue.js自定义指令Vue.js过渡和动画Vue.js混入Vue.js自定义事件和v-model......
  • JS逆向 -- 某资源网站下载验证绕过
    一、当搜索资源的时候,有时会遇到类似的情况,就是关注微信某公众号好,回复相应的内容,获取下载地址二、随便输入一个假的验证码,提示了验证码错误三、F12打开开发者工具,点击右上方的三个点,选择搜索,全局搜索“验证码错误”四、双击进入代码段,里面有个1219,输入验证码,点击提交查看五、成功......
  • Three.js教程:高光网格材质Phong
    推荐:将NSDT场景编辑器加入你的3D工具链其他系列工具:NSDT简石数字孪生高光网格材质Phong高光网格材质MeshPhongMaterial和基础网格材质MeshBasicMaterial、漫反射网格材质MeshLambertMaterial一样都是网格模型的Mesh的材质。高光网格材质MeshPhongMaterial和漫反射网格材质Mesh......
  • js中new Map ( )的使用场景
    当有一组数据:letarr=[{name:'钢筋',value:11},{name:'水泥',value:12},{name:'混凝土',value:13},{name:'加气砖',value:14}]后台返回了一个13,但是我们想要拿到的是混凝土,一般的做法:arr.forEach(item=>{if(item.value==......
  • js:highlight.js实现代码的语法高亮
    highlight.jsSyntaxhighlightingfortheWeb译文:highlight.js为Web突出显示语法文档https://highlightjs.org/使用方式1、方式一:cdn<linkrel="stylesheet"href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-da......
  • jstack Dump 日志文件分析
    jstackDump日志文件中的线程状态dump文件里,值得关注的线程状态有:1.死锁,Deadlock(重点关注)2.Runnable3.等待资源,Waitingoncondition(重点关注)4.Waitingonmonitorentry(重点关注)5.暂停,Suspended6.Object.wait()或TIMED_WAITING7.阻塞,Blocked(重点......
  • 遍历Json
    privatevoidSetShpFcSaveC5s(ShpFcSavemodel){if(string.IsNullOrWhiteSpace(model.C5)==false){JsonDocumentdocument=JsonDocument.Parse(model.C5);foreach(JsonElementjsonElementindocument.RootElement.EnumerateArray())......