先上结论:

用new执行构造器函数时:1.若构造器函数没有返回值,则默认返回this;2.若构造器函数返回非对象的值,则返回this;3.若构造器函数返回对象,则返回该对象

        var Test1=function(){
            this.name="wang";
            return {birth:"1995"};
        }
        Test1.prototype.phone="12345";
        console.log(new Test1());

        console.log("///////////////////////////////////////////////////////////////");
        var Test2=function(){
            this.name="wang";
            return "hello";
        }
        Test2.prototype.phone="12345";
        console.log(new Test2());

第一部分代码返回一个带有birth属性的对象,但没有name属性和phone属性。若将返回值注释掉,则返回带有name和phone属性的对象,但没有birth属性

第二部分代码返回带有name和phone属性的对象。返回的字符串并没有卵用。

此结论在ES6中class的constructor函数同样适用

分类: javascript

11 条评论

g so · 2020年6月20日 上午9:46

Useful information. Lucky me I found your web site by accident, and I’m stunned why this coincidence didn’t happened in advance!

I bookmarked it.

think g · 2020年6月21日 上午8:01

Thank you a lot for sharing this with all people you actually realize what
you’re talking about! Bookmarked. Please also seek advice from
my website =). We may have a hyperlink change arrangement between us

www.minerva.gallery · 2020年6月26日 上午10:24

Hi there to every one, as I am actually eager of
reading this blog’s post to be updated regularly.

It includes pleasant data.

213.246.61.113 · 2020年6月27日 上午2:21

These are actually wonderful ideas in regarding blogging. You
have touched some good points here. Any way keep up wrinting.

tinyurl.com · 2020年6月27日 上午2:53

I’d like to thank you for the efforts you’ve put in writing this website.
I really hope to see the same high-grade content
from you in the future as well. In fact, your creative
writing abilities has motivated me to get my very own site now 😉

http://tinyurl.com/ · 2020年6月27日 上午7:09

Excellent beat ! I wish to apprentice while you amend your site, how can i
subscribe for a blog website? The account helped me a
acceptable deal. I had been tiny bit acquainted of this your broadcast provided
bright clear concept

referenciasmedicas.com · 2020年6月27日 上午7:54

Hi there, yup this piece of writing is truly good and I have learned lot of things from it on the topic of blogging.

thanks.

cbd oil that works 2020 · 2020年6月27日 下午3:16

My brother suggested I might like this web site.
He was totally right. This post actually made my day.
You can not imagine simply how much time I had
spent for this information! Thanks!

cbd oil that works 2020 · 2020年6月28日 上午12:38

We are a group of volunteers and opening a new scheme in our community.

Your site provided us with valuable info to
work on. You’ve done an impressive job and our entire
community will be thankful to you.

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

Hello everyone, it’s my first pay a visit at this web page, and post
is truly fruitful in support of me, keep up posting such articles or reviews.

cbd oil that works 2020 · 2020年6月28日 上午7:18

Hey there, You’ve done an incredible job. I’ll definitely digg it and personally recommend to my friends.
I am confident they’ll be benefited from this site.

发表评论

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