js里的js继承方式是怎样的

在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。
问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
define(['jquery'],functions($){
function A(options){
this.options =
this.init();
A.prototype.getData = function(){
//do something
A.prototype.init = function(){
var self =
$(function(){
self.getData();
define(['jquery'],functions($){
function B(options){
this.options =
this.init();
B.prototype.getData = function(){
//do something
B.prototype.init = function(){
var self =
$(function(){
self.getData();
B如何继承A,调用A里面的方法和值?
还是说用依赖就可以了,那依赖的话要怎么写?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
继承的部分和普通的JS实现继承的方法没有太大差别,你可以参考下面的实现方式,另外,如果用方式这种继承的话,init方法的调用位置是值得商榷的。
define(['jquery', 'a'], function ($, A) {
function B(options) {
this.options =
B.prototype = new A();
B.prototype.constructor = B;
//保存父类的引用
B.prototype.parent = A.
//复写A中的方法
B.prototype.getData = function () {
//do something
B.prototype.init = function () {
//如果需要的话,可以调用父类的方法
this.parent.init.call(this);
console.log('inited');
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
继承和调用依然用js的方式去做
我只会coffee 大概写成这个样子
define [],()-&
constructor: () -&
@init=()-&
@getData()
getData:()-&
console.log "I am A"
define ['a'],(A)-&
class B extends A
constructor: () -&
@init=()-&
console.log "I am B"
getData:()-&
index.coffee
# class B的构造函数 输出 `I am B`
# b调用父类的 getData() 输出 `I am A `
b.getData()
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
js的继承是使用 原理是使用原型链进行继承(prototype)
而模块化的原理是使用闭包进行访问权限控制,暴露出接口,当然现在有很多封装好的模块化类库,比如seajs,modjs,requirejs等等
同步到新浪微博
分享到微博?
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
在 SegmentFault,学习技能、解决问题
每个月,我们帮助 1000 万的开发者解决各种各样的技术问题。并助力他们在技术能力、职业生涯、影响力上获得提升。function parent(){
function son(){
son.prototype = new parent();
function Cat(name){
Animal.call(this);
this.name = name || 'Tom';
(function(){
// 创建一个没有实例方法的类
var Super = function(){};
Super.prototype = Animal.
//将实例作为子类的原型
Cat.prototype = new Super();
利用原型来继承,通过增加一个空的函数来避免原型链上的对象共享
{name:“小明”};
var b = cloneObj(a);
cloneObj(obj){
var f = function(){};
f.prototype =
return new
用构造函数和原型链的混合模式去实现继承,避免对象共享可以参考经典的extend()函数,很多前端框架都有封装的,就是用一个空函数当做中间变量
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
京ICP备号-4
扫一扫,把题目装进口袋浅谈js中的三种继承方式及其优缺点
转载 & & 投稿:jingxian
下面小编就为大家带来一篇浅谈js中的三种继承方式及其优缺点。小编觉得挺不错的,现在就分享给大家,也给大家做个参考,一起跟随小编过来看看吧
第一种,prototype的方式:
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.
function man(){
this.feature = ['beard','strong'];
man.prototype = new person();
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
这种方式最为简单,只需要让子类的prototype属性值赋值为被继承的一个实例就行了,之后就可以直接使用被继承类的方法了。
prototype 属性是啥意思呢? prototype 即为原型,每一个对象 ( 由 function 定义出来 ) 都有一个默认的原型属性,该属性是个对象类型。
并且该默认属性用来实现链的向上攀查。意思就是说,如果某个对象的属性不存在,那么将通过prototype属性所属对象来查找这个属性。如果 prototype 查找不到呢?
js会自动地找prototype的prototype属性所属对象来查找,这样就通过prototype一直往上索引攀查,直到查找到了该属性或者prototype最后为空 (“undefined”);
例如上例中的one.view()方法,js会先在one实例中查找是否有view()方法,因为没有,所以查找man.prototype属性,而prototype的值为person的一个实例,
该实例有view()方法,于是调用成功。
第二种,apply的方式:
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.
function man(){
// person.apply(this,new Array());
person.apply(this,[]);
this.feature = ['beard','strong'];
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
注意:如果apply参数为空,即没有参数传递,则通过 new Array() 、[] 来传递,null 无效。
第三种,call+prototype的方式:
function person(){
this.hair = 'black';
this.eye = 'black';
this.skin = 'yellow';
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.
function man(){
// person.apply(this,new Array());
person.call(this,[]);
this.feature = ['beard','strong'];
man.prototype = new person();
var one = new man();
console.log(one.feature); //['beard','strong']
console.log(one.hair); //black
console.log(one.eye); //black
console.log(one.skin); //yellow
console.log(one.view()); //black,black,yellow
call方式的实现机制却要多一条 man.prototype = new person(); 为啥呢?
那是因为call方法只实现了方法的替换而没有作对象属性的复制操作。
google Map API 的继承就是使用这种方式。
上面总结了三种继承方式的实现。但是每种方法都有其优缺点。
假如父类是这样的:
function person(hair,eye,skin){
this.hair =
this.eye =
this.skin =
this.view = function(){
return this.hair + ',' + this.eye + ',' + this.
子类应该如何设计,使子类man在创建对象的同时传递参数到父类person,prototype的继承方式就不适用了,
必须采用apply或者call的方式了:
//apply方式
function man(hair,eye,skin){
person.apply(this,[hair,eye,skin]);
this.feature = ['beard','strong'];
//call方式
function man(hair,eye,skin){
person.call(this,hair,eye,skin);
this.feature = ['beard','strong'];
但是用apply方法也还是有缺点的,为什么?在js中,我们有个非常重要的运算符就是”instanceof”,该运算符用来比较某个对向是否为某种类型。
对于这个例子,one实例除了是man类型,也应该是person类型,但是apply方式继承之后,one却不属于person类型,即(one instanceof person)的值为false。
经此种种,最好的继承方式就是call+prototype方式了,之后你可以试一下(one instanceof BaseClass)的值是否为true。
第三种继承方式也有缺陷:子类new对象时要传一遍父类所需的参数,而且会重现父类中的属性和方法,下面这种继承方式才是完善的:
function Person(name){
this.name =
Person.prototype.getName = function() {
return this.
function Chinese(name, nation) {
Person.call(this, name);
this.nation =
//继承方法
function inherit(subClass, superClass) {
function F() {}
F.prototype = superClass.
subClass.prototype = new F();
subClass.prototype.constructor = subClass.
inherit(Chinese, Person);
Chinese.prototype.getNation = function() {
return this.
var p = new Person('shijun');
var c = new Chinese("liyatang", "China");
console.log(p); // Person {name: "shijun", getName: function}
console.log(c); // Chinese {name: "liyatang", nation: "China", constructor: function, getNation: function, getName: function}
console.log(p.constructor); // function Person(name){}
console.log(c.constructor); // function Chinese(){}
console.log(c instanceof Chinese); // true
console.log(c instanceof Person); // true
以上这篇浅谈js中的三种继承方式及其优缺点就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具JavaScript是如何实现继承的(六种方式)
转载 & & 作者:Evan_Chen
大多OO语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现,下文给大家技术js实现继承的六种方式,需要的朋友参考下
前言:大多OO语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠 原型链 来实现。
基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。
构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。
原型链实现继承例子:
function SuperType() {
this.property =
SuperType.prototype.getSuperValue = function() {
return this.
function subType() {
this.property =
//继承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this.
var instance = new SubType();
console.log(instance.getSuperValue());//true
2.借用构造函数
基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。
function SuperType() {
this.colors = ["red","blue","green"];
function SubType() {
SuperType.call(this);//继承了SuperType
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"
3.组合继承
基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。
function SuperType(name) {
this.name =
this.colors = ["red","blue","green"];
SuperType.prototype.sayName = function() {
console.log(this.name);
function SubType(name, age) {
SuperType.call(this,name);//继承属性
this.age =
//继承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = S
Subtype.prototype.sayAge = function() {
console.log(this.age);
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20
4.原型式继承
基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。
原型式继承的思想可用以下函数来说明:
function object(o) {
function F(){}
F.prototype =
return new F();
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
5.寄生式继承
基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"
6.寄生组合式继承
基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法
其基本模型如下所示:
function inheritProperty(subType, superType) {
var prototype = object(superType.prototype);//创建对象
prototype.constructor = subT//增强对象
subType.prototype =//指定对象
function SuperType(name){
this.name =
this.colors = ["red","blue","green"];
SuperType.prototype.sayName = function (){
alert(this.name);
function SubType(name,age){
SuperType.call(this,name);
this.age =
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
以上内容给大家介绍了javascript实现继承的六种方式,希望对大家有所帮助!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 js继承 的文章

 

随机推荐