Why does isPrototypeOf() return false?__proto__ VS. prototype in JavaScript[[Prototype]] vs prototype: ..what is the difference? (MyCons.__proto__ === MyCons.prototype) equals FALSEHow does JavaScript .prototype work?What does “use strict” do in JavaScript, and what is the reasoning behind it?event.preventDefault() vs. return falseWhat is JSONP, and why was it created?Why does Google prepend while(1); to their JSON responses?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?Detecting a mobile browserIs Safari on iOS 6 caching $.ajax results?How do I return the response from an asynchronous call?
Shrinkwrap tetris shapes without scaling or diagonal shapes
What is the incentive for curl to release the library for free?
Checks user level and limit the data before saving it to mongoDB
French for 'It must be my imagination'?
Unexpected email from Yorkshire Bank
Sci-fi book: portals appear in London and send a failed artist towards a designated path where he operate a giant superweapon
Error message with tabularx
Mac Pro install disk keeps ejecting itself
Binary Numbers Magic Trick
Uniformly continuous derivative implies existence of limit
What do the phrase "Reeyan's seacrest" and the word "fraggle" mean in a sketch?
How did Captain America manage to do this?
If a warlock with the Repelling Blast invocation casts Eldritch Blast and hits, must the targets always be pushed back?
Is DC-DC (24v to 12v) Buck Conversion typically more efficient than AC-DC (110v to 12v) conversion?
Are Boeing 737-800’s grounded?
Stop and Take a Breath!
How to creep the reader out with what seems like a normal person?
What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?
Pass By Reference VS Pass by Value
What's the polite way to say "I need to urinate"?
Term for maladaptive animal behavior that will lead to their demise?
How much cash can I safely carry into the USA and avoid civil forfeiture?
simple conditions equation
How come there are so many candidates for the 2020 Democratic party presidential nomination?
Why does isPrototypeOf() return false?
__proto__ VS. prototype in JavaScript[[Prototype]] vs prototype: ..what is the difference? (MyCons.__proto__ === MyCons.prototype) equals FALSEHow does JavaScript .prototype work?What does “use strict” do in JavaScript, and what is the reasoning behind it?event.preventDefault() vs. return falseWhat is JSONP, and why was it created?Why does Google prepend while(1); to their JSON responses?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?Detecting a mobile browserIs Safari on iOS 6 caching $.ajax results?How do I return the response from an asynchronous call?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the below constructors and SubType prototype pointing to an instance of SuperType. When I do x.isPrototypeOf(SubType.prototype)
it returns false
. I am confused as I have explicitly set x
as a prototype for SubType
. Can someone tell me why this is happening?
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
javascript
add a comment |
I have the below constructors and SubType prototype pointing to an instance of SuperType. When I do x.isPrototypeOf(SubType.prototype)
it returns false
. I am confused as I have explicitly set x
as a prototype for SubType
. Can someone tell me why this is happening?
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
javascript
2
Not sure to get my head all clear here, butx === SubType.prototype
how do you expect it to be its own prototype?
– Kaiido
Apr 24 at 2:21
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
tryconsole.log(x.isPrototypeOf(new SubType))
for example of how it's used.
– dandavis
Apr 24 at 2:29
Notice that you better should be usingx = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13
add a comment |
I have the below constructors and SubType prototype pointing to an instance of SuperType. When I do x.isPrototypeOf(SubType.prototype)
it returns false
. I am confused as I have explicitly set x
as a prototype for SubType
. Can someone tell me why this is happening?
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
javascript
I have the below constructors and SubType prototype pointing to an instance of SuperType. When I do x.isPrototypeOf(SubType.prototype)
it returns false
. I am confused as I have explicitly set x
as a prototype for SubType
. Can someone tell me why this is happening?
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
console.log(x.isPrototypeOf(SubType)) // returns false
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
javascript
javascript
edited Apr 24 at 12:58
Boann
37.7k1291123
37.7k1291123
asked Apr 24 at 1:56
GautamGautam
610413
610413
2
Not sure to get my head all clear here, butx === SubType.prototype
how do you expect it to be its own prototype?
– Kaiido
Apr 24 at 2:21
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
tryconsole.log(x.isPrototypeOf(new SubType))
for example of how it's used.
– dandavis
Apr 24 at 2:29
Notice that you better should be usingx = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13
add a comment |
2
Not sure to get my head all clear here, butx === SubType.prototype
how do you expect it to be its own prototype?
– Kaiido
Apr 24 at 2:21
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
tryconsole.log(x.isPrototypeOf(new SubType))
for example of how it's used.
– dandavis
Apr 24 at 2:29
Notice that you better should be usingx = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13
2
2
Not sure to get my head all clear here, but
x === SubType.prototype
how do you expect it to be its own prototype?– Kaiido
Apr 24 at 2:21
Not sure to get my head all clear here, but
x === SubType.prototype
how do you expect it to be its own prototype?– Kaiido
Apr 24 at 2:21
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
try
console.log(x.isPrototypeOf(new SubType))
for example of how it's used.– dandavis
Apr 24 at 2:29
try
console.log(x.isPrototypeOf(new SubType))
for example of how it's used.– dandavis
Apr 24 at 2:29
Notice that you better should be using
x = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Notice that you better should be using
x = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13
add a comment |
3 Answers
3
active
oldest
votes
SubType
is a function. What you probably want to check is if an instance of SubType would inherit from x
:
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
add a comment |
It helps to add properties to the objects to see what's happening. I fixed a little of your code. You can run this in the console:
function SuperType(foo) this.foo = foo ;
function SubType(bar) this.bar = bar ;
var x = new SubType("bar");
SuperType.prototype = x;
SuperType.prototype.constructor = SubType;
Now, you asked x.isPrototypeOf(SuperType)
and it returns false, because x
is not a property of the class SuperType
. But when you instantiate a SuperType
, x
is a property of that new object:
var y = new SuperType("foo");
console.log(x.isPrototypeOf(y)) // returns true
In your example that is true, SubType.prototype
is a prototype of SuperType.prototype
and returns true.
console.log(SubType.prototype.isPrototypeOf(SuperType.prototype)) // returns true
New contributor
add a comment |
I think that this is misunderstanding of prototype
and __proto__
properties.
Lets modify example above a little
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false.
// Now most interesting piece
SubType.__proto__ = x;
console.log(x.isPrototypeOf(SubType)) // Now true.
Demo
In place of SubType.__proto__ = x
we may use Object.setPrototypeOf(SubType, x)
which will yeilds the same result
The trik is __proto__
holds real object which is prototype. prototype
is used only in constructor fucntions for defining prototype for constructed objects. (See here)
So if we again modify first example
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false
var x2 = new SubType();
console.log(x.isPrototypeOf(x2)) // returns true
Demo
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55821319%2fwhy-does-isprototypeof-return-false%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
SubType
is a function. What you probably want to check is if an instance of SubType would inherit from x
:
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
add a comment |
SubType
is a function. What you probably want to check is if an instance of SubType would inherit from x
:
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
add a comment |
SubType
is a function. What you probably want to check is if an instance of SubType would inherit from x
:
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
SubType
is a function. What you probably want to check is if an instance of SubType would inherit from x
:
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
SubType.prototype.constructor = SubType;
const instance = new SubType();
console.log(x.isPrototypeOf(instance)) // returns true
console.log(SuperType.prototype.isPrototypeOf(SubType.prototype)) // returns true
answered Apr 24 at 2:29
KaiidoKaiido
46.6k469110
46.6k469110
add a comment |
add a comment |
It helps to add properties to the objects to see what's happening. I fixed a little of your code. You can run this in the console:
function SuperType(foo) this.foo = foo ;
function SubType(bar) this.bar = bar ;
var x = new SubType("bar");
SuperType.prototype = x;
SuperType.prototype.constructor = SubType;
Now, you asked x.isPrototypeOf(SuperType)
and it returns false, because x
is not a property of the class SuperType
. But when you instantiate a SuperType
, x
is a property of that new object:
var y = new SuperType("foo");
console.log(x.isPrototypeOf(y)) // returns true
In your example that is true, SubType.prototype
is a prototype of SuperType.prototype
and returns true.
console.log(SubType.prototype.isPrototypeOf(SuperType.prototype)) // returns true
New contributor
add a comment |
It helps to add properties to the objects to see what's happening. I fixed a little of your code. You can run this in the console:
function SuperType(foo) this.foo = foo ;
function SubType(bar) this.bar = bar ;
var x = new SubType("bar");
SuperType.prototype = x;
SuperType.prototype.constructor = SubType;
Now, you asked x.isPrototypeOf(SuperType)
and it returns false, because x
is not a property of the class SuperType
. But when you instantiate a SuperType
, x
is a property of that new object:
var y = new SuperType("foo");
console.log(x.isPrototypeOf(y)) // returns true
In your example that is true, SubType.prototype
is a prototype of SuperType.prototype
and returns true.
console.log(SubType.prototype.isPrototypeOf(SuperType.prototype)) // returns true
New contributor
add a comment |
It helps to add properties to the objects to see what's happening. I fixed a little of your code. You can run this in the console:
function SuperType(foo) this.foo = foo ;
function SubType(bar) this.bar = bar ;
var x = new SubType("bar");
SuperType.prototype = x;
SuperType.prototype.constructor = SubType;
Now, you asked x.isPrototypeOf(SuperType)
and it returns false, because x
is not a property of the class SuperType
. But when you instantiate a SuperType
, x
is a property of that new object:
var y = new SuperType("foo");
console.log(x.isPrototypeOf(y)) // returns true
In your example that is true, SubType.prototype
is a prototype of SuperType.prototype
and returns true.
console.log(SubType.prototype.isPrototypeOf(SuperType.prototype)) // returns true
New contributor
It helps to add properties to the objects to see what's happening. I fixed a little of your code. You can run this in the console:
function SuperType(foo) this.foo = foo ;
function SubType(bar) this.bar = bar ;
var x = new SubType("bar");
SuperType.prototype = x;
SuperType.prototype.constructor = SubType;
Now, you asked x.isPrototypeOf(SuperType)
and it returns false, because x
is not a property of the class SuperType
. But when you instantiate a SuperType
, x
is a property of that new object:
var y = new SuperType("foo");
console.log(x.isPrototypeOf(y)) // returns true
In your example that is true, SubType.prototype
is a prototype of SuperType.prototype
and returns true.
console.log(SubType.prototype.isPrototypeOf(SuperType.prototype)) // returns true
New contributor
New contributor
answered Apr 24 at 2:43
David KlingeDavid Klinge
3346
3346
New contributor
New contributor
add a comment |
add a comment |
I think that this is misunderstanding of prototype
and __proto__
properties.
Lets modify example above a little
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false.
// Now most interesting piece
SubType.__proto__ = x;
console.log(x.isPrototypeOf(SubType)) // Now true.
Demo
In place of SubType.__proto__ = x
we may use Object.setPrototypeOf(SubType, x)
which will yeilds the same result
The trik is __proto__
holds real object which is prototype. prototype
is used only in constructor fucntions for defining prototype for constructed objects. (See here)
So if we again modify first example
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false
var x2 = new SubType();
console.log(x.isPrototypeOf(x2)) // returns true
Demo
add a comment |
I think that this is misunderstanding of prototype
and __proto__
properties.
Lets modify example above a little
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false.
// Now most interesting piece
SubType.__proto__ = x;
console.log(x.isPrototypeOf(SubType)) // Now true.
Demo
In place of SubType.__proto__ = x
we may use Object.setPrototypeOf(SubType, x)
which will yeilds the same result
The trik is __proto__
holds real object which is prototype. prototype
is used only in constructor fucntions for defining prototype for constructed objects. (See here)
So if we again modify first example
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false
var x2 = new SubType();
console.log(x.isPrototypeOf(x2)) // returns true
Demo
add a comment |
I think that this is misunderstanding of prototype
and __proto__
properties.
Lets modify example above a little
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false.
// Now most interesting piece
SubType.__proto__ = x;
console.log(x.isPrototypeOf(SubType)) // Now true.
Demo
In place of SubType.__proto__ = x
we may use Object.setPrototypeOf(SubType, x)
which will yeilds the same result
The trik is __proto__
holds real object which is prototype. prototype
is used only in constructor fucntions for defining prototype for constructed objects. (See here)
So if we again modify first example
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false
var x2 = new SubType();
console.log(x.isPrototypeOf(x2)) // returns true
Demo
I think that this is misunderstanding of prototype
and __proto__
properties.
Lets modify example above a little
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false.
// Now most interesting piece
SubType.__proto__ = x;
console.log(x.isPrototypeOf(SubType)) // Now true.
Demo
In place of SubType.__proto__ = x
we may use Object.setPrototypeOf(SubType, x)
which will yeilds the same result
The trik is __proto__
holds real object which is prototype. prototype
is used only in constructor fucntions for defining prototype for constructed objects. (See here)
So if we again modify first example
function SuperType()
function SubType()
x = new SuperType();
SubType.prototype = x;
console.log(x.isPrototypeOf(SubType)) // returns false
var x2 = new SubType();
console.log(x.isPrototypeOf(x2)) // returns true
Demo
answered Apr 24 at 8:22
FyodorFyodor
1708
1708
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55821319%2fwhy-does-isprototypeof-return-false%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Not sure to get my head all clear here, but
x === SubType.prototype
how do you expect it to be its own prototype?– Kaiido
Apr 24 at 2:21
Updated my question, sorry about that type
– Gautam
Apr 24 at 2:23
try
console.log(x.isPrototypeOf(new SubType))
for example of how it's used.– dandavis
Apr 24 at 2:29
Notice that you better should be using
x = Object.create(SuperType.prototype)
– Bergi
Apr 24 at 7:12
Possible duplicate of [[Prototype]] vs prototype: ..what is the difference?
– Bergi
Apr 24 at 7:13