ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 构造器属性 ## 1.constructor属性 使用constructor属性可以获取到创建对象使用的构造器函数对象,所以我们可以通过判断构造器的类型来得知创建的对象的类型(找父亲) ```javascript function Dog(name,age,sex) { this.name = name; this.age = age; this.sex = sex; } var dog1 = new Dog("wc",17,1); if(dog1.constructor == Dog){ /*找父亲*/ console.log("dog1 是Dog的儿子"); } ``` ## 2.instanceof关键字 直接判断指定的对象类型,找得到返回的是true,找不到返回的是false ```javascript functionAnimal(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } var wc =newAnimal("wc",3,1); console.log(wc); functionPerson(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } var per \=newPerson("lw",18,0); console.log(per); console.log(wc instanceofAnimal);/\*true\*/ console.log(per instanceofPerson);/\*true\*/ console.log(per instanceofAnimal);/\*false\*/ ```