前提
关于在指趣学院上JS相关课程的小笔记共享。
获取对象真实类型
一般来讲,当我们说到获取输出数据类型一般想到的是用typeof,但是有时候用typeof输出的数据类型面对构造函数时依旧会是object,此时我们可以用到:
console.log(p.constructor.name)
instanceof关键字详解
instanceof关键字:用于判断“对象”是否为指定函数的“实例”。
输出true或false
使用instanceof关键字的注意点:
只要构造函数的原型对象出现在实例对象的原型链中都会返回true
function Person(myName){
this.name=myName;
}
function Student(myName,myScore){
Person.call(this,myName);//为了继承Person,把this和myName传给它
this.score=myScore;
}
Student.prototype=new Person();//为了使用父类原型的方法,把子类原型对象设置为父类的实例对象
Student.prototype.constuctor=Student;
let stu=new Student();
console.log(stu instanceof Person); //true
isPrototypeOf关键字详解
instanceof关键字:用于判断一个对象是否为另一个对象的原型
输出true或false
使用isPrototypeOf关键字的注意点:
function Person(myName){
this.name=myName;
}
function Student(myName,myScore){
Person.call(this,myName);//为了继承Person,把this和myName传给它
this.score=myScore;
}
Student.prototype=new Person();//为了使用父类原型的方法,把子类原型对象设置为父类的实例对象
Student.prototype.constuctor=Student;
let stu=new Student();
console.log(Person.prototype.isPrototypeOf(stu)); //true