Why can't i set the 'prototype' of a function created using 'bind'?Event binding on dynamically created...

Why do single electrical receptacles exist?

What is formjacking?

What could cause an entire planet of humans to become aphasic?

Do the speed limit reductions due to pollution also apply to electric cars in France?

Have any astronauts or cosmonauts died in space?

Is there any danger of my neighbor having my wife's signature?

Will the duration of traveling to Ceres using the same tech developed for going to Mars be proportional to the distance to go to Mars or not?

What does "south of due west" mean?

Why is Shelob considered evil?

Tikz: Perpendicular FROM a line

How to deal with an underperforming subordinate?

What is the meaning of "usr"?

Problems with position of tikzpictures in beamer

If I tried and failed to start my own business, how do I apply for a job without job experience?

Coworker is trying to get me to sign his petition to run for office. How to decline politely?

Missing a connection and don't have money to book next flight

Coworker asking me to not bring cakes due to self control issue. What should I do?

Why write a book when there's a movie in my head?

How can I prevent an oracle who can see into the past from knowing everything that has happened?

In a post apocalypse world, with no power and few survivors, would Satnav still work?

Does Plato's "Ring of Gyges" have a corrupting influence on its wearer?

1990s-2000s horror alien movie with slugs infecting people through the mouth

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Why is it that Bernie Sanders is always called a "socialist"?



Why can't i set the 'prototype' of a function created using 'bind'?


Event binding on dynamically created elements?Set a default parameter value for a JavaScript functionUnderstanding the difference between Object.create() and new SomeFunction()What is the difference between assigning a function via “this” vs. “prototype”?Advantages of setting the “constructor” Property in the “prototype”Loop inside React JSXprototype attribute [[prototype]] vs prototype property in functions?JavaScript prototypes equivalance for functionsCan't bind to 'ngModel' since it isn't a known property of 'input'Property added to a JS object as a prototype is hoisted, whereas a prototype function is not













6















consider the code:



function foo(something) {
this.a = something;
}

var obj1 = {};

var bar = foo.bind(obj1);


now the following statement doesn't execute:



bar.prototype.newprop = "new";//cannot execute this


as i understood,every function has a prototype object. then why can't we execute the above statement.
and bar is indeed a function as we can call it:



bar(2);
console.log(obj1.a); // 2









share|improve this question

























  • What are you trying to do by adding a prototype property to that function?

    – Pointy
    1 hour ago











  • @Pointy just trying to understand prototypes and bind

    – vikrant
    1 hour ago
















6















consider the code:



function foo(something) {
this.a = something;
}

var obj1 = {};

var bar = foo.bind(obj1);


now the following statement doesn't execute:



bar.prototype.newprop = "new";//cannot execute this


as i understood,every function has a prototype object. then why can't we execute the above statement.
and bar is indeed a function as we can call it:



bar(2);
console.log(obj1.a); // 2









share|improve this question

























  • What are you trying to do by adding a prototype property to that function?

    – Pointy
    1 hour ago











  • @Pointy just trying to understand prototypes and bind

    – vikrant
    1 hour ago














6












6








6


3






consider the code:



function foo(something) {
this.a = something;
}

var obj1 = {};

var bar = foo.bind(obj1);


now the following statement doesn't execute:



bar.prototype.newprop = "new";//cannot execute this


as i understood,every function has a prototype object. then why can't we execute the above statement.
and bar is indeed a function as we can call it:



bar(2);
console.log(obj1.a); // 2









share|improve this question
















consider the code:



function foo(something) {
this.a = something;
}

var obj1 = {};

var bar = foo.bind(obj1);


now the following statement doesn't execute:



bar.prototype.newprop = "new";//cannot execute this


as i understood,every function has a prototype object. then why can't we execute the above statement.
and bar is indeed a function as we can call it:



bar(2);
console.log(obj1.a); // 2






javascript prototype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







vikrant

















asked 1 hour ago









vikrantvikrant

448613




448613













  • What are you trying to do by adding a prototype property to that function?

    – Pointy
    1 hour ago











  • @Pointy just trying to understand prototypes and bind

    – vikrant
    1 hour ago



















  • What are you trying to do by adding a prototype property to that function?

    – Pointy
    1 hour ago











  • @Pointy just trying to understand prototypes and bind

    – vikrant
    1 hour ago

















What are you trying to do by adding a prototype property to that function?

– Pointy
1 hour ago





What are you trying to do by adding a prototype property to that function?

– Pointy
1 hour ago













@Pointy just trying to understand prototypes and bind

– vikrant
1 hour ago





@Pointy just trying to understand prototypes and bind

– vikrant
1 hour ago












4 Answers
4






active

oldest

votes


















9















as i understood, every function has a prototype object




Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.



In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are




  • arrow functions (() => {…})

  • methods (method() { … } in object literals and classes)

  • builtin non-constructor functions (like Math.sin)






share|improve this answer
























  • Can you tell us, when to use bind method instead of call and apply.

    – Bear Nithi
    4 mins ago



















4














See the specification:




Function.prototype.bind ( thisArg , ...args)



[...]



NOTE 1 Function objects created using Function.prototype.bind are exotic objects. They also do not have a prototype property.







share|improve this answer































    3














    The returned function from .bind() has no prototype object. You can give it one:



    bar.prototype = { newprop: "new" };


    It is not true that "every function has a prototype object". Every function can have a prototype object, but the value of the "prototype" property can be anything, including null or undefined.



    Additionally, there are "special" functions that may not behave like ordinary functions in all cases.






    share|improve this answer































      2














      Adding properties to the prototype means you want to create an object by using the function as a constructor.



      When you create an object like by calling new on a function, this value is the new object being created. So it makes no sense to bind this to another value






      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%2f54842450%2fwhy-cant-i-set-the-prototype-of-a-function-created-using-bind%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9















        as i understood, every function has a prototype object




        Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.



        In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are




        • arrow functions (() => {…})

        • methods (method() { … } in object literals and classes)

        • builtin non-constructor functions (like Math.sin)






        share|improve this answer
























        • Can you tell us, when to use bind method instead of call and apply.

          – Bear Nithi
          4 mins ago
















        9















        as i understood, every function has a prototype object




        Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.



        In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are




        • arrow functions (() => {…})

        • methods (method() { … } in object literals and classes)

        • builtin non-constructor functions (like Math.sin)






        share|improve this answer
























        • Can you tell us, when to use bind method instead of call and apply.

          – Bear Nithi
          4 mins ago














        9












        9








        9








        as i understood, every function has a prototype object




        Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.



        In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are




        • arrow functions (() => {…})

        • methods (method() { … } in object literals and classes)

        • builtin non-constructor functions (like Math.sin)






        share|improve this answer














        as i understood, every function has a prototype object




        Well, there are exceptions to every rule :-) You found one: bound functions don't have a .prototype property because they don't need it. When you call a bound function with new, it calls the original function as a constructor, using the original's .prototype object as the prototype of the new instance.



        In fact, since ES6 many functions don't have a .prototype property with an object, because they are not constructors - they cannot be called with new so they don't need it. Among those are




        • arrow functions (() => {…})

        • methods (method() { … } in object literals and classes)

        • builtin non-constructor functions (like Math.sin)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        BergiBergi

        373k58561889




        373k58561889













        • Can you tell us, when to use bind method instead of call and apply.

          – Bear Nithi
          4 mins ago



















        • Can you tell us, when to use bind method instead of call and apply.

          – Bear Nithi
          4 mins ago

















        Can you tell us, when to use bind method instead of call and apply.

        – Bear Nithi
        4 mins ago





        Can you tell us, when to use bind method instead of call and apply.

        – Bear Nithi
        4 mins ago













        4














        See the specification:




        Function.prototype.bind ( thisArg , ...args)



        [...]



        NOTE 1 Function objects created using Function.prototype.bind are exotic objects. They also do not have a prototype property.







        share|improve this answer




























          4














          See the specification:




          Function.prototype.bind ( thisArg , ...args)



          [...]



          NOTE 1 Function objects created using Function.prototype.bind are exotic objects. They also do not have a prototype property.







          share|improve this answer


























            4












            4








            4







            See the specification:




            Function.prototype.bind ( thisArg , ...args)



            [...]



            NOTE 1 Function objects created using Function.prototype.bind are exotic objects. They also do not have a prototype property.







            share|improve this answer













            See the specification:




            Function.prototype.bind ( thisArg , ...args)



            [...]



            NOTE 1 Function objects created using Function.prototype.bind are exotic objects. They also do not have a prototype property.








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            strstr

            17.8k65579




            17.8k65579























                3














                The returned function from .bind() has no prototype object. You can give it one:



                bar.prototype = { newprop: "new" };


                It is not true that "every function has a prototype object". Every function can have a prototype object, but the value of the "prototype" property can be anything, including null or undefined.



                Additionally, there are "special" functions that may not behave like ordinary functions in all cases.






                share|improve this answer




























                  3














                  The returned function from .bind() has no prototype object. You can give it one:



                  bar.prototype = { newprop: "new" };


                  It is not true that "every function has a prototype object". Every function can have a prototype object, but the value of the "prototype" property can be anything, including null or undefined.



                  Additionally, there are "special" functions that may not behave like ordinary functions in all cases.






                  share|improve this answer


























                    3












                    3








                    3







                    The returned function from .bind() has no prototype object. You can give it one:



                    bar.prototype = { newprop: "new" };


                    It is not true that "every function has a prototype object". Every function can have a prototype object, but the value of the "prototype" property can be anything, including null or undefined.



                    Additionally, there are "special" functions that may not behave like ordinary functions in all cases.






                    share|improve this answer













                    The returned function from .bind() has no prototype object. You can give it one:



                    bar.prototype = { newprop: "new" };


                    It is not true that "every function has a prototype object". Every function can have a prototype object, but the value of the "prototype" property can be anything, including null or undefined.



                    Additionally, there are "special" functions that may not behave like ordinary functions in all cases.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    PointyPointy

                    318k44458521




                    318k44458521























                        2














                        Adding properties to the prototype means you want to create an object by using the function as a constructor.



                        When you create an object like by calling new on a function, this value is the new object being created. So it makes no sense to bind this to another value






                        share|improve this answer




























                          2














                          Adding properties to the prototype means you want to create an object by using the function as a constructor.



                          When you create an object like by calling new on a function, this value is the new object being created. So it makes no sense to bind this to another value






                          share|improve this answer


























                            2












                            2








                            2







                            Adding properties to the prototype means you want to create an object by using the function as a constructor.



                            When you create an object like by calling new on a function, this value is the new object being created. So it makes no sense to bind this to another value






                            share|improve this answer













                            Adding properties to the prototype means you want to create an object by using the function as a constructor.



                            When you create an object like by calling new on a function, this value is the new object being created. So it makes no sense to bind this to another value







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            YoukouleleYYoukouleleY

                            2,5981826




                            2,5981826






























                                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%2f54842450%2fwhy-cant-i-set-the-prototype-of-a-function-created-using-bind%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

                                Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

                                Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                                How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...