构造函数:构造一个类(对象)的函数(使用new关键字调用的函数是构造函数)
特点:1. 首字母大写
2. 函数内部使用this关键字,谁new(实例)就指向谁(实例:new出来的对象)
3. 使用this关键字给对象添加属性
4. 必须使用new关键字,去生成一个对象
# this 指向实例化的对象
// 在 javascript 中新建一个类 使用构造函数/*** 学生类 name age skill* sayName()*/function Student(name, age, skill) {this.name = name;this.age = age;this.skill = skill;}Student.prototype.sayName = function(){console.log(this.name);}Student.prototype.saySkill = function(){console.log(this.skill);}/* this 指实例化的对象 *//* 实例 */var stu = new Student('雨与星星',22,'敲代码');console.log(stu);// 读取对象的属性console.log(stu.name);console.log(stu.age);//调用对象的方法stu.sayName();stu.saySkill();
instanceof 判断一个对象是不是某个类的实例
var arr = [1,2,3]console.log(arr instanceof Array); // truefunction Person(name,age){this.name = namethis.age = age}var p = new Person("zheng",18)console.log(p instanceof Person); // true
