分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

js重新讲解继承,es5的一些继承,es6继承的改变 ?----------由浅入深

发布时间:2023-09-06 01:10责任编辑:傅花花关键词:jses6

es5 利用原型公有私有继承

function Parent(name) { ???this.name = name}Parent.prototype.home = ‘北京‘;function Child() { ???this.age = 8;}//将父类的实例绑定在子类的原型上Child.prototype = new Parent(‘aa‘);//实例化原型let child = new Child();//这时候继承父类的公有私有属性console.log(child.home+child.name);// 结果 北京 + aa

es5 公有继承,改变this指向,私有不继承

function Parent(name) { ???this.name = name}//父类的公有属性Parent.prototype.home = ‘北京‘;function Child(name) { ???this.age = 8; ???Parent.call(this,...arguments);}let child = new Child(‘hello‘);console.log(child.home+child.name);//结果是 北京 +undefined

es5继承公有属性,私有属性不继承

function Parent(name) { ???this.name = name}Parent.prototype.home = ‘北京‘;function Child() { ???this.age = 8;}Child.prototype = Object.create(Parent.prototype);let child = new Child();//这时候继承只继承父类的公有属性 父类的私有属性没有继承console.log(child.home+child.name);// 结果 北京 + indefined

es6 继承,私有公有都继承extend//定义一个类

class Person { ???//控制器 ???constructor(name,age){ ???????this.name=name; ???????this.age=age; ???} ???eat(){ ???????console.log("吃饭"); ???}}//只继承共有的//关键字 extends 公私有都会继承class Girl extends ?Person{ ???constructor(name,age){ ???????super(name,age); //默认调用父类,并且this就是girl实例 ???}}let g=new Girl(‘aa‘,18);console.log(g.name,g.age);g.eat();
//结果aa 18

//
吃饭
 

 node继承  利用util模块  

inherits

function Parent(name) { ???this.name = name}Parent.prototype.home = ‘北京‘;function Child() { ???this.age = 8;}// es5 里面判断数据类型// indexof// instanceof// constuctor// Object.prototype.toString.call//util 判断数据类型console.log(util.isArray({}));//继承使用util里的方法 inheritsutil.inherits(Child,Parent);

js重新讲解继承,es5的一些继承,es6继承的改变 ?----------由浅入深

原文地址:http://www.cnblogs.com/null11/p/7509420.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved