Javascript prototype function undefined when constructor call in parentheses
Why does
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
bark at me that this.go is not a function whereas
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
is logging correctly as expected?
Execution environment is node v10.4.1 on macOS High Sierra 10.13.4
javascript
add a comment |
Why does
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
bark at me that this.go is not a function whereas
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
is logging correctly as expected?
Execution environment is node v10.4.1 on macOS High Sierra 10.13.4
javascript
1
As has been pointed out, it's a missing;
, May I advice you use an editor with built in linting, as things like this get picked up.
– Keith
Nov 16 '18 at 10:00
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18
add a comment |
Why does
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
bark at me that this.go is not a function whereas
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
is logging correctly as expected?
Execution environment is node v10.4.1 on macOS High Sierra 10.13.4
javascript
Why does
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
bark at me that this.go is not a function whereas
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
is logging correctly as expected?
Execution environment is node v10.4.1 on macOS High Sierra 10.13.4
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
(new Example());
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
new Example();
javascript
javascript
edited Nov 16 '18 at 9:52
Quentin
657k728941056
657k728941056
asked Nov 16 '18 at 9:50
On-The-Internet-Nobody-KnowsOn-The-Internet-Nobody-Knows
153
153
1
As has been pointed out, it's a missing;
, May I advice you use an editor with built in linting, as things like this get picked up.
– Keith
Nov 16 '18 at 10:00
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18
add a comment |
1
As has been pointed out, it's a missing;
, May I advice you use an editor with built in linting, as things like this get picked up.
– Keith
Nov 16 '18 at 10:00
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18
1
1
As has been pointed out, it's a missing
;
, May I advice you use an editor with built in linting, as things like this get picked up.– Keith
Nov 16 '18 at 10:00
As has been pointed out, it's a missing
;
, May I advice you use an editor with built in linting, as things like this get picked up.– Keith
Nov 16 '18 at 10:00
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18
add a comment |
3 Answers
3
active
oldest
votes
You are depending on ASI and it isn't working the way you expect.
In the first example, the ()
are turning the function expression you are trying to assign to Example.prototype.go
into an IIFE.
Consequently, the order of events is:
Example
is definednew Example()
is evaluated- The result is passed as an argument in a call to the anonymous function
- The return value of that function (
undefined
) is assigned toExample.prototype.go
… except it errors at step 2 because go
isn't defined at that point.
End each statement with an explicit semi-colon to avoid this.
add a comment |
It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (
. If you correctly terminate the assignment with a ;
, the problem goes away:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
The problem is that without the ;
, the ()
expression following the assignment combines with the function()
expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example
. So ASI doesn't kick in.
If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;
s.
If you are intentionally relying on ASI, you must start lines that have a leading (
or [
with a proactive ;
for this very reason. So in your case:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
;(new Example());
// ^----
add a comment |
The problem is that you forgot a semicolon after the function expression assigned to .go
:
Example.prototype.go = function()
console.log("going");
// <--- no semicolon!
(new Example());
The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:
Example.prototype.go = function()
console.log("going");
(new Example());
This is happening before Example.prototype.go
has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go
. But you're calling new Example()
before Example.prototype.go
is populated; thus, this.go
is undefined
.
Just put a semicolon after the }
instead:
Example.prototype.go = function()
console.log("going");
;
(new Example());
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%2f53335257%2fjavascript-prototype-function-undefined-when-constructor-call-in-parentheses%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
You are depending on ASI and it isn't working the way you expect.
In the first example, the ()
are turning the function expression you are trying to assign to Example.prototype.go
into an IIFE.
Consequently, the order of events is:
Example
is definednew Example()
is evaluated- The result is passed as an argument in a call to the anonymous function
- The return value of that function (
undefined
) is assigned toExample.prototype.go
… except it errors at step 2 because go
isn't defined at that point.
End each statement with an explicit semi-colon to avoid this.
add a comment |
You are depending on ASI and it isn't working the way you expect.
In the first example, the ()
are turning the function expression you are trying to assign to Example.prototype.go
into an IIFE.
Consequently, the order of events is:
Example
is definednew Example()
is evaluated- The result is passed as an argument in a call to the anonymous function
- The return value of that function (
undefined
) is assigned toExample.prototype.go
… except it errors at step 2 because go
isn't defined at that point.
End each statement with an explicit semi-colon to avoid this.
add a comment |
You are depending on ASI and it isn't working the way you expect.
In the first example, the ()
are turning the function expression you are trying to assign to Example.prototype.go
into an IIFE.
Consequently, the order of events is:
Example
is definednew Example()
is evaluated- The result is passed as an argument in a call to the anonymous function
- The return value of that function (
undefined
) is assigned toExample.prototype.go
… except it errors at step 2 because go
isn't defined at that point.
End each statement with an explicit semi-colon to avoid this.
You are depending on ASI and it isn't working the way you expect.
In the first example, the ()
are turning the function expression you are trying to assign to Example.prototype.go
into an IIFE.
Consequently, the order of events is:
Example
is definednew Example()
is evaluated- The result is passed as an argument in a call to the anonymous function
- The return value of that function (
undefined
) is assigned toExample.prototype.go
… except it errors at step 2 because go
isn't defined at that point.
End each statement with an explicit semi-colon to avoid this.
answered Nov 16 '18 at 9:54
QuentinQuentin
657k728941056
657k728941056
add a comment |
add a comment |
It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (
. If you correctly terminate the assignment with a ;
, the problem goes away:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
The problem is that without the ;
, the ()
expression following the assignment combines with the function()
expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example
. So ASI doesn't kick in.
If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;
s.
If you are intentionally relying on ASI, you must start lines that have a leading (
or [
with a proactive ;
for this very reason. So in your case:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
;(new Example());
// ^----
add a comment |
It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (
. If you correctly terminate the assignment with a ;
, the problem goes away:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
The problem is that without the ;
, the ()
expression following the assignment combines with the function()
expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example
. So ASI doesn't kick in.
If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;
s.
If you are intentionally relying on ASI, you must start lines that have a leading (
or [
with a proactive ;
for this very reason. So in your case:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
;(new Example());
// ^----
add a comment |
It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (
. If you correctly terminate the assignment with a ;
, the problem goes away:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
The problem is that without the ;
, the ()
expression following the assignment combines with the function()
expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example
. So ASI doesn't kick in.
If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;
s.
If you are intentionally relying on ASI, you must start lines that have a leading (
or [
with a proactive ;
for this very reason. So in your case:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
;(new Example());
// ^----
It's because you're relying on Automatic Semicolon Insertion (ASI) (perhaps unintentionally), but also starting a line with a (
. If you correctly terminate the assignment with a ;
, the problem goes away:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
The problem is that without the ;
, the ()
expression following the assignment combines with the function()
expression in front of it in a syntactically- (but not logically-) valid way: It calls the function, passing in the result of new Example
. So ASI doesn't kick in.
If you aren't intentionally relying on ASI, just note that assignment expressions need to be terminated with ;
s.
If you are intentionally relying on ASI, you must start lines that have a leading (
or [
with a proactive ;
for this very reason. So in your case:
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
;(new Example());
// ^----
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
function Example()
this.go();
Example.prototype.go = function()
console.log("going");
; // <==== ; here
(new Example());
answered Nov 16 '18 at 9:54
T.J. CrowderT.J. Crowder
698k12312401336
698k12312401336
add a comment |
add a comment |
The problem is that you forgot a semicolon after the function expression assigned to .go
:
Example.prototype.go = function()
console.log("going");
// <--- no semicolon!
(new Example());
The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:
Example.prototype.go = function()
console.log("going");
(new Example());
This is happening before Example.prototype.go
has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go
. But you're calling new Example()
before Example.prototype.go
is populated; thus, this.go
is undefined
.
Just put a semicolon after the }
instead:
Example.prototype.go = function()
console.log("going");
;
(new Example());
add a comment |
The problem is that you forgot a semicolon after the function expression assigned to .go
:
Example.prototype.go = function()
console.log("going");
// <--- no semicolon!
(new Example());
The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:
Example.prototype.go = function()
console.log("going");
(new Example());
This is happening before Example.prototype.go
has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go
. But you're calling new Example()
before Example.prototype.go
is populated; thus, this.go
is undefined
.
Just put a semicolon after the }
instead:
Example.prototype.go = function()
console.log("going");
;
(new Example());
add a comment |
The problem is that you forgot a semicolon after the function expression assigned to .go
:
Example.prototype.go = function()
console.log("going");
// <--- no semicolon!
(new Example());
The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:
Example.prototype.go = function()
console.log("going");
(new Example());
This is happening before Example.prototype.go
has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go
. But you're calling new Example()
before Example.prototype.go
is populated; thus, this.go
is undefined
.
Just put a semicolon after the }
instead:
Example.prototype.go = function()
console.log("going");
;
(new Example());
The problem is that you forgot a semicolon after the function expression assigned to .go
:
Example.prototype.go = function()
console.log("going");
// <--- no semicolon!
(new Example());
The following line beings with opening parentheses - thus, the interpreter thinks you're immediately calling the aforementioned function:
Example.prototype.go = function()
console.log("going");
(new Example());
This is happening before Example.prototype.go
has been assigned to - the interpreter is trying to evaluate the right-hand side of the expression to a value before assigning the result to Example.prototype.go
. But you're calling new Example()
before Example.prototype.go
is populated; thus, this.go
is undefined
.
Just put a semicolon after the }
instead:
Example.prototype.go = function()
console.log("going");
;
(new Example());
answered Nov 16 '18 at 9:55
CertainPerformanceCertainPerformance
97.3k165887
97.3k165887
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%2f53335257%2fjavascript-prototype-function-undefined-when-constructor-call-in-parentheses%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
1
As has been pointed out, it's a missing
;
, May I advice you use an editor with built in linting, as things like this get picked up.– Keith
Nov 16 '18 at 10:00
Following the advice now :-) Thanx.
– On-The-Internet-Nobody-Knows
Nov 18 '18 at 11:18