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;








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?










share|improve this question






















  • I'd argue that it doesn't actually work

    – Bergi
    Mar 26 at 7:30

















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?










share|improve this question






















  • I'd argue that it doesn't actually work

    – Bergi
    Mar 26 at 7:30













0












0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












3 Answers
3






active

oldest

votes


















0














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();








share|improve this answer






























    0














    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();








    share|improve this answer






























      0














      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.






      share|improve this answer

























        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
        );



        );













        draft saved

        draft discarded


















        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









        0














        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();








        share|improve this answer



























          0














          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();








          share|improve this answer

























            0












            0








            0







            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();








            share|improve this answer













            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();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 at 3:46









            brkbrk

            32.3k3 gold badges25 silver badges47 bronze badges




            32.3k3 gold badges25 silver badges47 bronze badges























                0














                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();








                share|improve this answer



























                  0














                  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();








                  share|improve this answer

























                    0












                    0








                    0







                    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();








                    share|improve this answer













                    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();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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





















                        0














                        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.






                        share|improve this answer



























                          0














                          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.






                          share|improve this answer

























                            0












                            0








                            0







                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 26 at 3:58









                            82Tuskers82Tuskers

                            1,0876 silver badges12 bronze badges




                            1,0876 silver badges12 bronze badges



























                                draft saved

                                draft discarded
















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript