对象的方法补充
◼ hasOwnProperty
对象是否有某一个属于自己的属性(不是在原型上的属性)
◼ in/for in 操作符
判断某个属性是否在某个对象或者对象的原型上
◼ instanceof
用于检测构造函数(Person、Student类)的pototype,是否出现在某个实例对象的原型链上
◼ isPrototypeOf
用于检测某个对象,是否出现在某个实例对象的原型链上
案例:
<script src="./js/inherit_untils.js"></script>
<script>
var obj = {
name:"hdc",
age:21
}
var info = createObject(obj)
info.address = "中国"
info.intro = "中国大好河山"
console.log(info.name,info.address)
console.log(info)
console.log(info.hasOwnProperty("name"))
console.log(info.hasOwnProperty("address"))
console.log("name" in info)
console.log("address" in info)
for (var key in info){
console.log(key)
}
function Person(){}
function Student(){}
inherit(Student,Person)
var stu = new Student()
console.log(stu instanceof Student)
console.log(stu instanceof Person)
console.log(stu instanceof Object)
console.log(stu instanceof Array)
console.log(Student.prototype.isPrototypeOf(stu))
console.log(Person.prototype.isPrototypeOf(stu))
console.log(obj.isPrototypeOf(info))
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构