Prototype : difference between using constructor and just object itselfWhat is the reason to use the 'new' keyword at Derived.prototype = new BaseWhat techniques can be used to define a class in JavaScript, and what are their trade-offs?Difference between == and === in JavaScriptHow does JavaScript .prototype work?What's the difference between using “let” and “var”?What is the difference between call and apply?Understanding the difference between Object.create() and new SomeFunction()What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Differences between lodash and underscoreWhat is the difference between Bower and npm?Trying to understand the difference between prototype and constructor in JavaScript
Is an acid a salt or not?
Are neural networks prone to catastrophic forgetting?
Who Can Help Retag This?
What does "it kind of works out" mean?
Flatten array with OPENJSON: OPENJSON on a value that may not be an array? [ [1] ], vs [1]
how to convert unix epoch microseconds into Java time stamp
Managing and organizing the massively increased number of classes after switching to SOLID?
Can I call 112 to check a police officer's identity in the Czech Republic?
Is there a word for a message that is intended to be intercepted by an adversary?
Why are Hobbits so fond of mushrooms?
Double-sided action figure, early 80s?
How do Windows version numbers work?
Cops: The Hidden OEIS Substring
Is anyone advocating the promotion of homosexuality in UK schools?
Rhombuses, kites etc
Why do players in the past play much longer tournaments than today's top players?
Cubic programming and beyond?
Why do Americans say "less than five people"?
Matchmaker, Matchmaker, make me a match
Why isn't pressure filtration popular compared to vacuum filtration?
During copyediting, journal disagrees about spelling of paper's main topic
What's an appropriate title for a person who deals with conflicts of an Empire?
Leave PhD after one year or finish it to the end?
Is Trump personally blocking people on Twitter?
Prototype : difference between using constructor and just object itself
What is the reason to use the 'new' keyword at Derived.prototype = new BaseWhat techniques can be used to define a class in JavaScript, and what are their trade-offs?Difference between == and === in JavaScriptHow does JavaScript .prototype work?What's the difference between using “let” and “var”?What is the difference between call and apply?Understanding the difference between Object.create() and new SomeFunction()What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Differences between lodash and underscoreWhat is the difference between Bower and npm?Trying to understand the difference between prototype and constructor in JavaScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm studying javascript prototype.
CASE 01 - works!
var A = function()
A.prototype.hello = function()
console.log('hello');
var B = function()
B.prototype = new A();
var C = function ()
C.prototype = new B();
var c = new C();
c.hello();
CASE 02 - doesn't work!
var A =
hello : function()
console.log('hello');
var B =
B.prototype = A;
var C =
C.prototype = B;
C.hello();
I think I didn't understand the concept of prototype and constructor or how the objects inherit each other. Can anybody explain why CASE 02 doesn't work?
javascript
add a comment |
I'm studying javascript prototype.
CASE 01 - works!
var A = function()
A.prototype.hello = function()
console.log('hello');
var B = function()
B.prototype = new A();
var C = function ()
C.prototype = new B();
var c = new C();
c.hello();
CASE 02 - doesn't work!
var A =
hello : function()
console.log('hello');
var B =
B.prototype = A;
var C =
C.prototype = B;
C.hello();
I think I didn't understand the concept of prototype and constructor or how the objects inherit each other. Can anybody explain why CASE 02 doesn't work?
javascript
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30
add a comment |
I'm studying javascript prototype.
CASE 01 - works!
var A = function()
A.prototype.hello = function()
console.log('hello');
var B = function()
B.prototype = new A();
var C = function ()
C.prototype = new B();
var c = new C();
c.hello();
CASE 02 - doesn't work!
var A =
hello : function()
console.log('hello');
var B =
B.prototype = A;
var C =
C.prototype = B;
C.hello();
I think I didn't understand the concept of prototype and constructor or how the objects inherit each other. Can anybody explain why CASE 02 doesn't work?
javascript
I'm studying javascript prototype.
CASE 01 - works!
var A = function()
A.prototype.hello = function()
console.log('hello');
var B = function()
B.prototype = new A();
var C = function ()
C.prototype = new B();
var c = new C();
c.hello();
CASE 02 - doesn't work!
var A =
hello : function()
console.log('hello');
var B =
B.prototype = A;
var C =
C.prototype = B;
C.hello();
I think I didn't understand the concept of prototype and constructor or how the objects inherit each other. Can anybody explain why CASE 02 doesn't work?
javascript
javascript
asked Mar 26 at 3:39
LemileLemile
203 bronze badges
203 bronze badges
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30
add a comment |
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30
add a comment |
3 Answers
3
active
oldest
votes
B.prototype
is just going to be a key on B
object since B.prototype = A
is going to create a key name prototype
on B
. Same happens with C
, it will create a nested key of prototype
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
add a comment |
I can not really explain, but for me it's just a question of syntax:
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
add a comment |
I think the point to note is that prototype is a property of function AND not an object. Case 2 does NOT work mainly because of this reason.
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%2f55349514%2fprototype-difference-between-using-constructor-and-just-object-itself%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
B.prototype
is just going to be a key on B
object since B.prototype = A
is going to create a key name prototype
on B
. Same happens with C
, it will create a nested key of prototype
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
add a comment |
B.prototype
is just going to be a key on B
object since B.prototype = A
is going to create a key name prototype
on B
. Same happens with C
, it will create a nested key of prototype
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
add a comment |
B.prototype
is just going to be a key on B
object since B.prototype = A
is going to create a key name prototype
on B
. Same happens with C
, it will create a nested key of prototype
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
B.prototype
is just going to be a key on B
object since B.prototype = A
is going to create a key name prototype
on B
. Same happens with C
, it will create a nested key of prototype
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
var A =
hello: function()
console.log('hello');
var B =
B.prototype = A;
console.log(B)
var C =
C.prototype = B;
console.log(C)
C.prototype.prototype.hello();
answered Mar 26 at 3:46
brkbrk
32.3k3 gold badges25 silver badges47 bronze badges
32.3k3 gold badges25 silver badges47 bronze badges
add a comment |
add a comment |
I can not really explain, but for me it's just a question of syntax:
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
add a comment |
I can not really explain, but for me it's just a question of syntax:
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
add a comment |
I can not really explain, but for me it's just a question of syntax:
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
I can not really explain, but for me it's just a question of syntax:
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
var A =
hello : function()
console.log('hello');
var X = Object.create(A);
X.hello();
answered Mar 26 at 3:54
Mister JojoMister Jojo
2,2832 gold badges2 silver badges18 bronze badges
2,2832 gold badges2 silver badges18 bronze badges
add a comment |
add a comment |
I think the point to note is that prototype is a property of function AND not an object. Case 2 does NOT work mainly because of this reason.
add a comment |
I think the point to note is that prototype is a property of function AND not an object. Case 2 does NOT work mainly because of this reason.
add a comment |
I think the point to note is that prototype is a property of function AND not an object. Case 2 does NOT work mainly because of this reason.
I think the point to note is that prototype is a property of function AND not an object. Case 2 does NOT work mainly because of this reason.
answered Mar 26 at 3:58
82Tuskers82Tuskers
1,0876 silver badges12 bronze badges
1,0876 silver badges12 bronze badges
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%2f55349514%2fprototype-difference-between-using-constructor-and-just-object-itself%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
I'd argue that it doesn't actually work
– Bergi
Mar 26 at 7:30