Prototype in Javascript

I am learning about Javascript prototype. The program is suppose to have output: true. However, I am stuck at setting one of the condition true. Consider this code:

var xCount;
var x = {};
var y = new Object();
var z = [];


Object.prototype.mySpecialProperty= true;

for (xCount = 1; xCount < 100 && x == x.constructor; xCount++)
{
   x = x.constructor;
}

print("Result of five tests on prototypes and inheritance: " + (
xCount == 100
&& Object.getPrototypeOf(Object.getPrototypeOf(y)) == null
&& Object.getPrototypeOf(Object.getPrototypeOf(z)) === Object.prototype
&& Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(z).constructor)) === Object.prototype
&& x.mySpecialProperty && y.mySpecialProperty && z.mySpecialProperty));

Open in new window


I thought if I set x = {} then x.constructor = Object and will equal x since x is Object. However it is not the case. Could you provide a solution and the explanation for this problem. Thanks
LVL 1
Dung TrinhMarching Band Field CrewAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
I don't know what exactly you're trying to achieve here but the only false condition is the first:
xCount == 100

The value of xCount is 1, not 100.
This is due to the loop exit condition: xCount < 100 && x == x.constructor
So you have to set the x = x.contructor before entering the loop.

You can do it a bit smarter by setting x as follows:
var x = new function(){
    this.constructor = this;
};

Open in new window

Here's my running test code
0
Dung TrinhMarching Band Field CrewAuthor Commented:
It work but is there a simpler way to set x = x.constructor? My goal is setting x = x.constructor .
0
Alexandre SimõesManager / Technology SpecialistCommented:
Here's an alternative, but you have to set x as a function.
var xCount;
var x = function(){};

var y = new Object();
var z = [];

Object.prototype.mySpecialProperty= true;

x = x.constructor;
for (xCount = 1; xCount < 100 && x == x.constructor; xCount++)
{
   x = x.constructor;
}

Open in new window


Here's my updates demo

Still this exercise is too academic for my taste.
Doesn't make much sense in a practical usage.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dung TrinhMarching Band Field CrewAuthor Commented:
Thanks. It is the solution.
I know it is just academic but that is what you get when you study in school. The classes try to teach the academic side of the language. The practical part stay in the project
0
Alexandre SimõesManager / Technology SpecialistCommented:
Ok, if this is actually school material it makes "more" sense.
I thought you were learning it on your own from some sort of training material.

At the end it's cool, I like this kind of "puzzles" :)

Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.