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?
what to look for in luxury cars like Acura/Lexus
Purpose of のは in this sentence?
Why is this entry signal showing green?
How do I tell my manager that his code review comment is wrong?
What was the design of the Macintosh II's MMU replacement?
How to change lightning-radio-group selected value programmatically?
how to ban all connection to .se and .ru in the hosts.deny-file
How to model the curly cable part of the phone
Missing Piece of Pie - Can you find it?
Out of scope work duties and resignation
What property of a BJT transistor makes it an amplifier?
Multi-channel audio upsampling interpolation
A mathematically illogical argument in the derivation of Hamilton's equation in Goldstein
Do Maps have an Reliable Relationship between keySet() order and values() order?
Would Hubble Space Telescope improve black hole image observed by EHT if it joined array of telesopes?
What are the differences between credential stuffing and password spraying?
How to apply differences on part of a list and keep the rest
Using field size much larger than necessary
As matter approaches a black hole, does it speed up?
Why is "Vayechulu" said 3 times on Leil Shabbat?
Why was the battle set up *outside* Winterfell?
Double or Take game
Have I damaged my car by attempting to reverse with hand/park brake up?
If I readied a spell with the trigger "When I take damage", do I have to make a constitution saving throw to avoid losing Concentration?
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 truejavascript
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 truejavascript
2
Not sure to get my head all clear here, butx === SubType.prototypehow 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 truejavascript
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 truefunction 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 truefunction 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 truejavascript
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.prototypehow 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.prototypehow 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 trueadd 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
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 trueadd 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 trueadd 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 trueSubType 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 truefunction 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 truefunction 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 trueanswered Apr 24 at 2:29
KaiidoKaiido
46.9k469111
46.9k469111
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
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
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
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
answered Apr 24 at 2:43
David KlingeDavid Klinge
3496
3496
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
31018
31018
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.prototypehow 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