Why can't i set the 'prototype' of a function created using 'bind'?Event binding on dynamically created...
Taking an academic pseudonym?
Is the percentage symbol a constant?
How do I avoid the "chosen hero" feeling?
Is Screenshot Time-tracking Common?
Why write a book when there's a movie in my head?
Protagonist constantly has to have long words explained to her. Will this get tedious?
What does "move past people" mean in this context?
Is it possible to detect 100% of SQLi with a simple regex?
Why can't i set the 'prototype' of a function created using 'bind'?
How can I differentiate duration vs starting time
Can you say "leftside right"?
Neglect higher order derivatives in expression
How many copper coins fit inside a cubic foot?
How can I handle players killing my NPC outside of combat?
How to deal with an underperforming subordinate?
How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?
Expression for "unconsciously using words (or accents) used by a person you often talk with or listen to"?
Could a civilization in medieval fantasy world sustain itself by occupying important trade hubs and taxing trade?
Using functions like sine, cosine and tangent to calculate coordinates in Tikz
Is there a way to pause a running process on Linux systems and resume later?
Why do single electrical receptacles exist?
Coworker asking me to not bring cakes due to self control issue. What should I do?
What is wrong with my use of "find -print0"?
Inline Feynman diagrams, Feynman diagrams in equations, very small Feynman diagrams
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
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
add a comment |
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
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
add a comment |
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
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
javascript prototype
edited 1 hour ago
vikrant
asked 1 hour ago
vikrantvikrant
453613
453613
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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
)
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
add a comment |
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 aprototype
property.
add a comment |
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.
add a comment |
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
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%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
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
)
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
add a comment |
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
)
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
add a comment |
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
)
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
)
answered 1 hour ago
BergiBergi
373k58561889
373k58561889
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
add a comment |
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
Can you tell us, when to use bind method instead of call and apply.
– Bear Nithi
7 mins ago
add a comment |
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 aprototype
property.
add a comment |
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 aprototype
property.
add a comment |
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 aprototype
property.
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 aprototype
property.
answered 1 hour ago
strstr
17.8k65579
17.8k65579
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 1 hour ago
PointyPointy
318k44458521
318k44458521
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 1 hour ago
YoukouleleYYoukouleleY
2,5981826
2,5981826
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%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
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
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