3-2 JS类、继承、instanceof

系列文章目录


目录

系列文章目录

前言

一、class

二、继承

三、类型判断-instanceof

总结


前言

JS原型相关


一、class

// 类
class Student {
    constructor(name, number) {
        this.name = name;
        this.number = number;
    }
    sayHi() {
        console.log(`姓名 ${this.name},学号 ${this.number}`);
    }
}
// 通过类 new 对象/实例
const cai = new Student('菜豆',100);
console.log(cai.name);
cai.sayHi();

二、继承

// 父类
class People {
    constructor(name) {
        this.name = name;
    }
    eat() {
        console.log(`${this.name} eat apple`);
    }
}

// 子类
class Student extents People {
    constructor(name, number) {
        super(name);
        this.number = number;
    }
    sayHi() {
        console.log(`姓名 ${this.name},学号 ${this.number}`);
    }
}

const cai = new Student('菜豆',100);
cai.sayHi();
cai.eat();

三、类型判断-instanceof

cai instanceof Student // true
cai instanceof People // true
cai instanceof Object // true

[] instanceof Array // true
[] instanceof Object // true

{} instanceof Object // true

原型关系

  • 每个 class 都有显示原型 prototype
  • 每个实例都有隐式原型 _proto_
  • 实例的 _proto_ 指向对应 class 的 prototype

基于原型的执行规则

  • 获取属性 cai.name 或执行方法 cai.sayHi() 时
  • 先在自身属性和方法寻找
  • 如果找不到则自动去 _proto_ 中查找

手写简易 jQuery 

class jQuery {
    constructor(selector) {
        const result = document.querySelectorAll(selector);
        const length = result.length;
        for(let i = 0; i < length; i++){
            this[i] = result[i];
        }
        this.length = length;
        this.selector = selector;
    }
    get(index) {
        return this[index];
    }
    each(fn) {
        for(let i = 0;i < this.length; i++) {
            const elem = this[i];
            fn(elem);
        }
    }
    on(type, fn) {
        return this.each(elem => {
            elem.addEventListener(type, fn, false);
        })
    }
    // 扩展...
}

<p>1</p>
<p>1</p>
<p>1</p>

const $p = new jQuery('p');
$p.get(1);
$p.each((elem) => console.log(elem.nodeName));
$p.on('click', () => alert('clicked'));

// 插件
jQuery.prototype.dialog = function (info) {
    alert(info);
}

// "造轮子"
class myJQuery extends jQuery {
    constructor(selector) {
        super(selector);
    }
    // 扩展自己的方法
    addClass(className) {
    }
    style(data){
    }
}


总结

类、继承、instanceof,原型相关学习笔记。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中,`instanceof`是一个用于检测对象是否是某个构造函数的实例的运算符。它会检查构造函数的原型是否存在于对象的原型链上。\[1\] 例如,`obj instanceof Object`会检测`Object.prototype`是否存在于`obj`的原型链上。如果存在,则返回`true`,否则返回`false`。\[2\] 下面是一些示例: 1. 未发生继承的情况下: - `f instanceof F`返回`true`,因为`F.prototype`存在于`f`的原型链上。 - `f instanceof H`返回`false`,因为`H.prototype`不存在于`f`的原型链上。 - `f instanceof Object`返回`true`,因为`Object.prototype`存在于`f`的原型链上。 - `F.prototype instanceof Object`返回`true`,因为`Object.prototype`存在于`F.prototype`的原型链上。 - `h instanceof F`返回`false`,因为`F.prototype`不存在于`h`的原型链上。 - `h instanceof H`返回`true`,因为`H.prototype`存在于`h`的原型链上。 - `h instanceof Object`返回`true`,因为`Object.prototype`存在于`h`的原型链上。 - `H.prototype instanceof Object`返回`true`,因为`Object.prototype`存在于`H.prototype`的原型链上。 2. 发生继承的情况下: - `f instanceof F`返回`true`,因为`F.prototype`存在于`f`的原型链上。 - `f instanceof H`返回`true`,因为`H.prototype`存在于`f`的原型链上。 - `f instanceof Object`返回`true`,因为`Object.prototype`存在于`f`的原型链上。 - `F.prototype instanceof Object`返回`true`,因为`Object.prototype`存在于`F.prototype`的原型链上。 - `h instanceof F`返回`false`,因为`F.prototype`不存在于`h`的原型链上。 - `h instanceof H`返回`true`,因为`H.prototype`存在于`h`的原型链上。 - `h instanceof Object`返回`true`,因为`Object.prototype`存在于`h`的原型链上。 - `H.prototype instanceof Object`返回`true`,因为`Object.prototype`存在于`H.prototype`的原型链上。\[3\] #### 引用[.reference_title] - *1* *3* [简说JS中的instanceof](https://blog.csdn.net/qq_44169219/article/details/103053889)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [js关于instanceof](https://blog.csdn.net/u013594477/article/details/121097270)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值