前端面试题整理——原型和原型链

Class的使用:

// 父类
    class People {
        constructor(name) {
            this.name = name
        }

        eat() {
            console.log(`${this.name} eat`)
        }
    }

    // 子类
    class Student extends People {
        constructor(name, number) {
            super(name);//super使用父类帮忙定义this.name
            this.number = number
        }

        sayHi() {
            console.log(`姓名:${this.name},学号:${this.number}`)
        }
    }

    const a = new Student('aaa',123)
    a.sayHi()
    a.eat()

View Code

类型判断-instance:

// 父类
    class People {
        constructor(name) {
            this.name = name
        }
        eat() {
            console.log(`${this.name} eat`)
        }
    }
    // 子类
    class Student extends People {
        constructor(name, number) {
            super(name);//super使用父类帮忙定义this.name
            this.number = number
        }
        sayHi() {
            console.log(`姓名:${this.name},学号:${this.number}`)
        }
    }
    const a = new Student('aaa',123)
    // a instanceof Student // true
    // a instanceof People // true
    // a instanceof Object // true

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

    // {} instanceof Object // true

View Code

原型:

// 父类
    class People {
        constructor(name) {
            this.name = name
        }
        eat() {
            console.log(`${this.name} eat`)
        }
    }
    // 子类
    class Student extends People {
        constructor(name, number) {
            super(name);//super使用父类帮忙定义this.name
            this.number = number
        }
        sayHi() {
            console.log(`姓名:${this.name},学号:${this.number}`)
        }
    }
    const a = new Student('aaa',123)

    // class 实际上是函数,是语法糖
    // typeof People // 'function'
    // typeof Student // 'function'

    // 隐式原型和显式原型
    console.log( a )
    console.log( a.__proto__ )
    console.log( Student.prototype )
    console.log( a.__proto__ === Student.prototype )

    // 原型关系
    // 每个class都有显式原型 prototype
    // 每个实例都有隐式原型 __proto__
    // 实例的__proto__指向对应class的prototype

    // 基于原型的执行规则
    // 先在自身属性和方法寻找
    // 如果找不到自动去__proto__寻找

View Code

原型链:

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

    // 子类
    class Student extends People {
        constructor(name, number) {
            super(name);//super使用父类帮忙定义this.name
            this.number = number
            this.rank = () => {
                console.log('rank 10')
            }
        }
        sayHi() {
            console.log(`姓名:${this.name},学号:${this.number}`)
        }
    }

    const a = new Student('aaa', 123)

    console.log(a);
    console.log(Student.__proto__.prototype);
    console.log(People.prototype);
    console.log(Student.__proto__.prototype === People.prototype); // true

    // hasOwnProperty() 方法来验证是否该对象自己的
    console.log(a.hasOwnProperty('name'));
    console.log(a.hasOwnProperty('sayHi'));
    console.log(a.hasOwnProperty('rank'));

    // 原型链直至找到Object的__proto__为止,Object的__proto__为null
    // instanceof的原理是原型链查找模式,一步步对照Class的显式原型是否存在

View Code

(0)

相关推荐

  • JS的原型和继承

    __proto__ 除null和undefined,JS中的所有数据类型都有这个属性: 它表示当我们访问一个对象的某个属性时,如果该对象自身不存在该属性, 就从它的__proto__属性上继续查找,以 ...

  • 万字长文深度剖析面向对象的javascript

    简介 本将会深入讲解面向对象在javascript中的应用,并详细介绍三种对象的生成方式:构造函数,原型链,类. 什么是对象 虽然说程序员不缺对象,随时随地都可以new一个出来,但是在程序的世界中,对 ...

  • JS原型链常见面试题分析

    构造函数: function Foo(name,age){ this.name=name; this.age=age; this.class='class-1'; } var f=new Foo('c ...

  • 创建对象及原型

    一.使用Object构造函数或对象字面量创建对象 缺点:使用同一个接口创建对象,会产生大量的重复性代码. 解决方法:使用工厂模式创建对象. 1 //构造函数2 let obj1 = new Objec ...

  • 干货 | 快速读懂 JS 原型链

    OSC开源社区 昨天 以下文章来源于前端日志 ,作者前端日志 前端日志前端一二线大厂日常工作.技术分享.实战总结. 最近参加了公司内部技术分享,分享同学提到了 Js 原型链的问题,并从 V8 的视角展 ...

  • 前端面试题整理——手写简易jquery

    class jQuery { constructor(selector) { const result = document.querySelectorAll(selector) console.lo ...

  • 前端面试题整理——深拷贝

    const obj = { a: 100, b: { b1: [1, 2, 3], b2: 'string' }, c: ['a', 'b', 'c'] } /* * 没做深拷贝的效果 const o ...

  • 前端面试题整理——手写bind函数

    var arr = [1,2,3,4,5] console.log(arr.slice(1,4)) console.log(arr) Function.prototype.bind1 = functi ...

  • 前端面试题整理——作用域和闭包

    什么是闭包,闭包的表现形式: // 作用域应用的特殊情况,有两种表现: // 函数作为参数被传递 // 函数作为返回值被返回 // 函数作为返回值 function create() { let a ...

  • 前端面试题整理——手写AJAX

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  • 前端面试题整理——VUE双向绑定原理

    VUE2.0和3.0数据双向绑定的原理,并说出其区别. 代码: <!DOCTYPE html> <html lang="en"> <head> ...

  • 前端面试题整理——Javascript基础

    常见值类型: let a; //undefined let s = 'abc'; let n = 100; let b = true; let sb = Symbol('s'); let nn = N ...

  • 前端面试题整理——HTML/CSS

    如何理解语义化: 对应的内容是用相应意思的标签,增加开发者和机器爬虫对代码的可读性. 块状元素和内联元素: 块状元素有:display:block/table:有div h1 h2 table ul  ...

  • 网络编程及前端面试题!Python入门

    当我们学习Python时,需要掌握的的知识有很多,除了有关Python的专业知识外,我们还需要学习网络编程.前端等知识,对此这篇文章为大家总结一下Python常见面试题之网络编程及前端的问题. 第一: ...