构造函数、原型

88 阅读1分钟

构造函数

//构造函数
function structure(name,no){
    this.name = name;
    this.no = no;
    this.methods = function(){
        document.write(`学号:${this.no} 姓名:${this.name} 爱好: 无 `);
    }
}

let stu1 = new structure("小明",1);
//调用方法执行
stu1.methods();

原型

每个函数都有一个prototype属性, 是一个指针,指向一个对象, 这个对象的用途是包含可以由特定实例共享的属性和方法

//构造函数
function structure(name,no){
    this.name = name;
    this.no = no;
}
structure.prototype.methods = function(){
        document.write(`学号:${this.no} 姓名:${this.name} 爱好: 无 `);
}
let stu1 = new structure("小明",1);
//调用方法执行
stu1.methods();

instanceof

判断一个实例是不是构造函数实例化出来的对象 返回true/false