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
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
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
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
4 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
4 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
4 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
4 mins ago
add a comment |
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
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