分享web开发知识

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

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

JS继承

发布时间:2023-09-06 01:40责任编辑:林大明关键词:暂无标签

JS继承

伪类:通过构造一个人伪类来继承某个构造器。通过定义它的constructor函数并替换它的prototype为某个构造器的实例。(类的继承)

//构造器var Mammal = function (name) { ???this.name = name; ???}Mammal.prototype.get_name = function () { ???return this.name; ?}Mammal.prototype.says = function () { ???return this.saying; ?}//继承var Cat = function (name) { ???this.name = name; ???this.saying = ‘meow‘; ?????}//替换Cat.prototype为一个新的Mammal实例Cat.prototype = new Mammal();

原型:新对象继承旧对象,通过创建一个对象并将其prototype指向目标对象。

// Object.creat ???Object.creat =function (o) { ???????var F= function () {}; ???????F.prototype=o; ???????return new F(); ????????}
//对象var myMammal ={ ???name : ‘ bss’, ???get_name : function (){ ???????return this.name;}, ???says : function () { ???????return this.saying || ‘ ‘;},} ??//继承对象var myCat = Object.creat(myMammal);

JS继承

原文地址:https://www.cnblogs.com/JhonFlame/p/8045809.html

知识推荐

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