Not able to use 'super' with function defined on protoype object in JavaScript classLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?What is the preferred syntax for defining enums in JavaScript?How do I test for an empty JavaScript object?How do I correctly clone a JavaScript object?Call a parent class's method from child class in Python?Checking if a key exists in a JavaScript object?
Why didn't Al Powell investigate the lights at the top of the building?
Why use null function instead of == []
Is a public company able to check out who owns its shares in very detailed format?
Could there exist a "locality" field?
Deep Learning based time series forecasting
Mistakenly modified `/bin/sh'
Does optical correction give a more aesthetic look to the SBI logo?
Are lithium batteries allowed in the International Space Station?
I quit, and boss offered me 3 month "grace period" where I could still come back
Doing research in academia and not liking competition
Is this more than a packing puzzle?
Behavior of the zero and negative/sign flags on classic instruction sets
I have accepted an internship offer. Should I inform companies I have applied to that have not gotten back to me yet?
Absconding a company after 1st day of joining
Can someone explain this logical statement?
How would you write do the dialogues of two characters talking in a chat room?
Why is "dark" an adverb in this sentence?
Should you avoid redundant information after dialogue?
Why does ffmpeg choose 10+20+20ms instead of an even 16ms for 60fps gifs?
How would someone destroy a black hole that’s at the centre of a planet?
Getting fresh water in the middle of hypersaline lake in the Bronze Age
Is killing off one of my queer characters homophobic?
What are the arguments for California’s nonpartisan blanket primaries other than giving Democrats more power?
Are there any double stars that I can actually see orbit each other?
Not able to use 'super' with function defined on protoype object in JavaScript class
Length of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How to change an element's class with JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?What is the preferred syntax for defining enums in JavaScript?How do I test for an empty JavaScript object?How do I correctly clone a JavaScript object?Call a parent class's method from child class in Python?Checking if a key exists in a JavaScript object?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have class A
which is parent of class B
class A
constructor(a)
this.a=a;
par()
console.log("para");
class B extends A
constructor(a)
super(a)
this.a = "child";
par()
super.par();
console.log("child");
When I use this code, it works fine.
But when I explicitly define the par
function on B
using this code:
B.prototype.par = function()
super.par();
I get the error
Uncaught SyntaxError: 'super' keyword unexpected here
Whether we create a function in class definition or in prototype object of function('class'), it should be the same thing.
What am I doing wrong here?
javascript class inheritance super
add a comment |
I have class A
which is parent of class B
class A
constructor(a)
this.a=a;
par()
console.log("para");
class B extends A
constructor(a)
super(a)
this.a = "child";
par()
super.par();
console.log("child");
When I use this code, it works fine.
But when I explicitly define the par
function on B
using this code:
B.prototype.par = function()
super.par();
I get the error
Uncaught SyntaxError: 'super' keyword unexpected here
Whether we create a function in class definition or in prototype object of function('class'), it should be the same thing.
What am I doing wrong here?
javascript class inheritance super
No, you can't usesuper
in afunction
. Just callA.prototype.par
.
– Bergi
Mar 26 at 7:27
add a comment |
I have class A
which is parent of class B
class A
constructor(a)
this.a=a;
par()
console.log("para");
class B extends A
constructor(a)
super(a)
this.a = "child";
par()
super.par();
console.log("child");
When I use this code, it works fine.
But when I explicitly define the par
function on B
using this code:
B.prototype.par = function()
super.par();
I get the error
Uncaught SyntaxError: 'super' keyword unexpected here
Whether we create a function in class definition or in prototype object of function('class'), it should be the same thing.
What am I doing wrong here?
javascript class inheritance super
I have class A
which is parent of class B
class A
constructor(a)
this.a=a;
par()
console.log("para");
class B extends A
constructor(a)
super(a)
this.a = "child";
par()
super.par();
console.log("child");
When I use this code, it works fine.
But when I explicitly define the par
function on B
using this code:
B.prototype.par = function()
super.par();
I get the error
Uncaught SyntaxError: 'super' keyword unexpected here
Whether we create a function in class definition or in prototype object of function('class'), it should be the same thing.
What am I doing wrong here?
javascript class inheritance super
javascript class inheritance super
edited Mar 26 at 7:37
TiiJ7
2,7573 gold badges18 silver badges29 bronze badges
2,7573 gold badges18 silver badges29 bronze badges
asked Mar 26 at 6:33
Suraj Prakash JoshiSuraj Prakash Joshi
881 silver badge7 bronze badges
881 silver badge7 bronze badges
No, you can't usesuper
in afunction
. Just callA.prototype.par
.
– Bergi
Mar 26 at 7:27
add a comment |
No, you can't usesuper
in afunction
. Just callA.prototype.par
.
– Bergi
Mar 26 at 7:27
No, you can't use
super
in a function
. Just call A.prototype.par
.– Bergi
Mar 26 at 7:27
No, you can't use
super
in a function
. Just call A.prototype.par
.– Bergi
Mar 26 at 7:27
add a comment |
1 Answer
1
active
oldest
votes
'super' is simply a syntactic sugar introduced in ES2015 along with class syntax.
It can only be used within functions of 'class' (constructor and methods) that extends another class.
class A
constructor()
par() console.log('para')
class B extends A
constructor()
super()
Is equivalent to:
function A()
A.prototype.par = function()console.log('para')
var B = (function(parent)
var _super = parent;
function B()
_super.call(this); // calls parent's constructor
B.prototype = Object.create(_super.prototype); // Inherits parent's methods.
B.prototype.par = function() // override parent's par.
_super.prototype.par.call(this); // child still has access to parent's par method thanks to closure :)
console.log('child');
return B;
)(A);
var b = new B();
b.par()
You cannot do:
function()
super // super is not defined...
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
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%2f55351075%2fnot-able-to-use-super-with-function-defined-on-protoype-object-in-javascript-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
'super' is simply a syntactic sugar introduced in ES2015 along with class syntax.
It can only be used within functions of 'class' (constructor and methods) that extends another class.
class A
constructor()
par() console.log('para')
class B extends A
constructor()
super()
Is equivalent to:
function A()
A.prototype.par = function()console.log('para')
var B = (function(parent)
var _super = parent;
function B()
_super.call(this); // calls parent's constructor
B.prototype = Object.create(_super.prototype); // Inherits parent's methods.
B.prototype.par = function() // override parent's par.
_super.prototype.par.call(this); // child still has access to parent's par method thanks to closure :)
console.log('child');
return B;
)(A);
var b = new B();
b.par()
You cannot do:
function()
super // super is not defined...
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
add a comment |
'super' is simply a syntactic sugar introduced in ES2015 along with class syntax.
It can only be used within functions of 'class' (constructor and methods) that extends another class.
class A
constructor()
par() console.log('para')
class B extends A
constructor()
super()
Is equivalent to:
function A()
A.prototype.par = function()console.log('para')
var B = (function(parent)
var _super = parent;
function B()
_super.call(this); // calls parent's constructor
B.prototype = Object.create(_super.prototype); // Inherits parent's methods.
B.prototype.par = function() // override parent's par.
_super.prototype.par.call(this); // child still has access to parent's par method thanks to closure :)
console.log('child');
return B;
)(A);
var b = new B();
b.par()
You cannot do:
function()
super // super is not defined...
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
add a comment |
'super' is simply a syntactic sugar introduced in ES2015 along with class syntax.
It can only be used within functions of 'class' (constructor and methods) that extends another class.
class A
constructor()
par() console.log('para')
class B extends A
constructor()
super()
Is equivalent to:
function A()
A.prototype.par = function()console.log('para')
var B = (function(parent)
var _super = parent;
function B()
_super.call(this); // calls parent's constructor
B.prototype = Object.create(_super.prototype); // Inherits parent's methods.
B.prototype.par = function() // override parent's par.
_super.prototype.par.call(this); // child still has access to parent's par method thanks to closure :)
console.log('child');
return B;
)(A);
var b = new B();
b.par()
You cannot do:
function()
super // super is not defined...
'super' is simply a syntactic sugar introduced in ES2015 along with class syntax.
It can only be used within functions of 'class' (constructor and methods) that extends another class.
class A
constructor()
par() console.log('para')
class B extends A
constructor()
super()
Is equivalent to:
function A()
A.prototype.par = function()console.log('para')
var B = (function(parent)
var _super = parent;
function B()
_super.call(this); // calls parent's constructor
B.prototype = Object.create(_super.prototype); // Inherits parent's methods.
B.prototype.par = function() // override parent's par.
_super.prototype.par.call(this); // child still has access to parent's par method thanks to closure :)
console.log('child');
return B;
)(A);
var b = new B();
b.par()
You cannot do:
function()
super // super is not defined...
edited Mar 26 at 8:58
answered Mar 26 at 6:47
dannyjeedannyjee
3382 silver badges7 bronze badges
3382 silver badges7 bronze badges
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
add a comment |
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
i have doubt in this line 'B.prototype = new super();'..what is it trying to do? because 'b.__proto_' will be equal to B.prototype
– Suraj Prakash Joshi
Mar 26 at 7:57
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
@Bergi You're right. It should be Object.create(A.prototype). I corrected the answer.
– dannyjee
Mar 26 at 8:58
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55351075%2fnot-able-to-use-super-with-function-defined-on-protoype-object-in-javascript-c%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
No, you can't use
super
in afunction
. Just callA.prototype.par
.– Bergi
Mar 26 at 7:27