// 构造函数继承
function Super(name) {
this.name = name;
}
function Son(name) {
Super.call(this, name)
}
let child = new Son('name');
console.log(child)
// 原型的继承 (一)
function createObj(obj) {
function F() {
}
F.prototype = obj;
return new F();
}
var obj = createObj({say: function () {
console.log(this.name)
}});
console.log(obj)
// 原型继承 (二)
var newObj = Object.create({say: function () {
console.log(this.name)
}});
console.log(newObj)
// 超类
function Super(name) {
this.name = name;
}
// 子类
function Child() {
Super.call(this, arguments)
}
// 子类的原型是Super的实例,实例通过proto寻找原型对象
Child.prototype = new Super();
// // 创建对象
// class Super {
// }
// // 实现继承
// class Son extends Super {
// }
function Super() {
}
Super.prototype = {
constructor: Super
}
var sup = new Super();
// console.log(sup instanceof Super); // true
// console.log(sup instanceof Object); // true
// console.log(Object.getPrototypeOf(sup)); // 获取原型
function myInstanceof(left, right) {
let proto = left.__proto__;
while(true) {
if (proto === null) return false;
if (proto === right.prototype) return true; // 因为左右两边指向同一个原型对象,所以会相等
proto = proto.__proto__;
}
}
console.log(myInstanceof(sup, Super))
console.log(myInstanceof(sup, Object))
怎么也想不到我居然有一天还要写简书
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。