如何在JavaScript中使用继承

  介绍

如何在JavaScript中使用继承?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

& lt; html>   ,,,& lt; head>   ,,,,,,,& lt; title> js函数继承进阶& lt;/title>   ,,,,,,,& lt; meta  charset=癠TF-8"/比;   ,,,,,,,& lt; script 类型=拔谋?javascript"比;   ,,,,,,,,,,,function 人(姓名、年龄){//才能对象的创建   ,,,,,,,,,,,,,,,this.name=名称;   ,,,,,,,,,,,,,,,this.age=年龄;//,,,,,,,,,,,,,,,,this.test=function () {//,,,,,,,,,,,,,,,,,,,,console.log(“能运行吗“);//,,,,,,,,,,,,,,,,}   ,,,,,,,,,,,}   ,,,,,,,,,,,function 书(名字){   ,,,,,,,,,,,,,,,this.name=笆俏衣稹?   ,,,,,,,,,,,,,,,this.lookat=function () {   ,,,,,,,,,,,,,,,,,,,console.log(“看看能行吗“);   ,,,,,,,,,,,,,,,}   ,,,,,,,,,,,}   ,,,,,,,,,,,function  czxt(名字){   ,,,,,,,,,,,,,,,this.name=名称;   ,,,,,,,,,,,}   ,,,,,,,,,,,person.prototype.read=function (a) {console.log(“小姐还要read");};   ,,,,,,,,,,,book.prototype.sign=function (a) {console.log(“小姐;am  book");, return  new 书()},,,,,//视其为一个对象的一个属性进行”继承”   ,,,,,,,,,,,person.prototype.book=new 书(),,,,,,,,,,,,,,,,//直接进行”包含“//,,,,,,,,,,,,person.prototype=new 书();   ,,,,,,,,,,,czxt.prototype.appear=function (a) {console.log(“我是操作系统“)};   ,,,,,,,,,,,book.prototype.subject=new  czxt ();//,,,,,,,,,,,,book.prototype=new  czxt ();   ,,,,,,,,,,,var  p1=new 人(“张三“,37);//,,,,,,,,,,,,p1.lookat(),,,,,//类内部的方法也可以调用//,,,,,,,,,,,,console.log (p1.name);,,,//包含类和父类都含有,是父类的名称属性值只,“张三“;   ,,,,,,,,,,,console.log (p1.book.name);,//用属性进行调用的话,是包含类的名称属性,,,“是我吗”//,,,,,,,,,,,,p1.sign(),,,,,,//直接调用包含函数方法//,,,,,,,,,,,,p1.book.sign();,,,//调用对象属性包含的方法//,,,,,,,,,,,,p1.read(),,,,,,//调用公共对象外函数//,,,,,,,,,,,,p1.test(),,,,,//函数调用函数内部的函数(视函数为对象)//,,,,,,,,,,,,p1.address=吧蕉?,,//每个特定对象主动添加对象属性//,,,,,,,,,,,,console.log (p1.address);   ,,,,,,,,,,,var  b1=new 书();//,,,,,,,,,,,,b1.appear();,,,//调用其继承体内的函数//,,,,,,,,,,,,p1.appear();,,,//无法调用包含对象内的包含方法(无法直接调用孙子函数)//,,,,,,,,,,,,p1.book.appear();,,//用属性调用也不行啊,不是函数可还行   null   null   null   null   null   null   null   null

如何在JavaScript中使用继承