因为最近面试的原因,有的面试官会问到”实现继承的几种方式”,我便到网上查了这个问题的答案。我认为网上说的几种继承方式都有一定的问题,我想在这里说一下我的观点。

首先,我认为,继承是类的继承,不存在所谓的对象的继承。但是我好像记得在《javascript语言精粹》一书中dc大神提过所谓对象继承,但那本书毕竟是多年前的书了,那时候js还没有类的概念,书中可能是说的原型链的关系,而且翻译也有可能有问题。总之我认为说对象的继承是不正确的,可以说对象的“扩展”,或者把对象作为原型链构造新的对象。

类的继承只有一种实现方法,那就是ES6的extend。ES5中也可以实现这中继承,省去细节,最关键的就是两句:(可以参考我以前的博客:babel类的继承实现分析

SubClass=function(...args){
// 继承原型链
SubClass.prototype=Object.create(superClass.prototype,{constructior:{value:subClass}});
// 执行构造函数
SubperClass.call(this,...args);
};

分析网上的几种错误继承

1.原型链继承

function SuperClass(){}
function SubClass(){}
SubClass.prototype=new SuperClass();

看似实现了继承,但是,问题:
* 父类中this上添加的属性方法一股脑给搞到了原型上。
* 原型上的constructor属性没了。
* 父类中对this的操作可能不能完全反映在子类this上。

2.借用构造函数继承

function SuperClass(){}
function SubClass(){
    SuperClass.call(this)
}

原型链呢?

3.组合继承

function SuperClass(){}
function SubClass(){
    SuperClass.call(this)
}
// ???
SubClass.prototype=new SuperClass();
SubClass.prototype.constructor=SubClass;

这个把前两种结合了起来,但是还是不对。只是把第一种方式原型上的constructor属性问题解决了,但是其他问题还是存在。关键在于SuperClass不应该在创建SubClass时调用new,而是只在SubClass实例化时调用new,而上面的代码它调用了两遍。

4.原型式继承

这里就开始所谓对象继承了

function object(o){
 function F(){}
 F.prototype = o;
 return new F();
}

你直接使用Object.create不好吗?黑人问号脸:)

5.寄生式继承

function createAnother(original) {
    var clone = object(original);
    clone.sayHi = function() {
        alert("hi");
    };
    return clone;
}

在第四种上又新写了一个方法。。。。。。

6.寄生组合式继承

function object(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

function inheritPrototype(superClass, subClass) {
    var prototype = object(superClass.prototype);
    prototype.constructor = subClass;
    subClass.prototype = prototype;
}

function SuperClass(name) {}
function SubClass(name, age) {
    SuperClass.call(this, name);
}

inheritPrototype(SuperClass, SubClass);

这个终于好像没什么毛病了。但是还是没有什么必要多写一个object函数。

分类: javascript

16 条评论

DavidSleda · 2020年3月16日 上午2:08

Hi! trenfash.cn

Did you know that it is possible to send commercial offer completely legal?
We offer a new legal method of sending request through feedback forms. Such forms are located on many sites.
When such messages are sent, no personal data is used, and messages are sent to forms specifically designed to receive messages and appeals.
Also, messages sent through communication Forms do not get into spam because such messages are considered important.
We offer you to test our service for free. We will send up to 50,000 messages for you.
The cost of sending one million messages is 49 USD.

This letter is created automatically. Please use the contact details below to contact us.

Contact us.
Telegram – @FeedbackFormEU
Skype live:feedbackform2019
Email – feedbackform@make-success.com

Charlesnob · 2020年5月28日 下午11:48

Good day, do you have a viable project that requires funding? Due to Covid-19, we offer loans at 1% interest. For more details, send message to info@simpleloanslimited.co.uk

Komalcax · 2020年6月10日 下午12:53

Greetings, I was just visiting your site and wondering about
the payment method currently offered by you to your customers?

I work for Amald.Com and we can serve your business by offering credit card and various local payment methods (eNets, Net Banking, Alipay, CUP, SEPA, SOFORT..etc) to accept online payments.

Our pricing start with 1.5% for Local Payments and 2.5% for
credit card payments.

Join free consultancy on info@amald.com for all your Payment Industry related queries.

Regards,
Komal

g some · 2020年6月20日 下午1:32

Spot on with this write-up, I seriously feel this website needs much more attention. I’ll probably be returning to see more,
thanks for the info!

them g · 2020年6月21日 上午4:08

This blog was… how do you say it? Relevant!! Finally I have found something which helped me.
Thanks!

g that · 2020年6月22日 上午2:32

Hi, all the time i used to check weblog posts here in the early hours in the daylight, as
i enjoy to gain knowledge of more and more.

John Brooks · 2020年6月22日 上午11:33

My name is John Brooks of AAA Structured Finance Ltd , Thank you for your time, my company offers project financing and lending services, do you have any project that requires funding at the moment? We are ready to work with you on a more transparent approach.

Best regards,
John Brooks
Principal Partner
AAA Structured Finance Ltd
E-mail: brooksjohn801@gmail.com

tinyurl.com · 2020年6月26日 上午10:57

I read this article fully about the comparison of most up-to-date and earlier
technologies, it’s amazing article.

http://ecoups.net/groups/cbd-oil-634298103/ · 2020年6月26日 下午8:10

I blog often and I genuinely appreciate your content. This article has really
peaked my interest. I will take a note of your blog and keep checking
for new details about once per week. I opted in for your RSS feed too.

http://tinyurl.com · 2020年6月27日 上午3:25

I read this paragraph completely concerning the difference of
newest and previous technologies, it’s remarkable article.

https://salmaexpress.com/author/eloyechols/ · 2020年6月27日 上午6:54

hello there and thank you for your information – I have certainly picked up something new
from right here. I did however expertise a few technical
issues using this site, since I experienced to reload the website lots of times previous to I
could get it to load properly. I had been wondering if your web hosting is OK?
Not that I’m complaining, but slow loading instances times will often affect
your placement in google and can damage your high
quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my e-mail and
can look out for a lot more of your respective exciting content.
Ensure that you update this again very soon.

http://tinyurl.com · 2020年6月27日 下午1:11

I got this web site from my buddy who informed me regarding
this web page and at the moment this time I am browsing this website and reading
very informative articles here.

cbd oil that works 2020 · 2020年6月27日 下午5:10

Fine way of describing, and good paragraph to take facts concerning my presentation subject matter, which i am going to convey in university.

cbd oil that works 2020 · 2020年6月27日 下午5:45

It is perfect time to make some plans for the future and it’s time to be happy.
I’ve read this post and if I could I want to suggest you few interesting things or suggestions.
Maybe you could write next articles referring to this article.
I want to read more things about it!

cbd oil that works 2020 · 2020年6月28日 上午11:05

It’s really a great and helpful piece of information. I
am glad that you just shared this helpful information with us.
Please keep us informed like this. Thanks for sharing.

cbd oil that works 2020 · 2020年6月29日 下午1:44

I like the helpful information you provide in your articles.
I will bookmark your weblog and check again here frequently.

I’m quite certain I will learn many new stuff right here!
Best of luck for the next!

发表评论

电子邮件地址不会被公开。 必填项已用*标注