javascript继承

来源:互联网 发布:西安电子科技大学网络 编辑:程序博客网 时间:2024/06/04 01:14

javascript继承

一、对象冒充

1.简单的对象冒充

//简单的对象冒充function Father(name){    this.name=name;    this.method= function(){        alert("father method!")    }}function Son(name){    this.fatherTemp = Father;    this.fatherTemp(name);    delete this.fatherTemp;}var son = new Son("son");alert(son.name);son.method();

2.call对象冒充

//使用call、apply方法的对象冒充function Father(name){    this.name=name;    this.method=function(){        alert("father method!")    }}function Son(name){    Father.call(this,name);}var son = new Son("son")alert(son.name);son.method();

二、原子性的对象冒充

function Father(name){    this.name=name;    this.method=function(){        alert("father method!")    }}function Son(){}Son.prototype = new Father("father");var son = new Son();alert(son.name);son.method();

三、对象继承的常用方式

function Father(name){    this.name=name;}Father.prototype.method=function(){    alert("father method!")}function Son(name){    Father.call(this,name)}Son.prototype = new Father();Son.prototype.show=function(){    alert("show")}var son = new Son();alert(son.name)son.method();son.show();
原创粉丝点击