Importance of Javascript Prototype Inheritance [duplicate]
This question already has an answer here:
How does JavaScript .prototype work?
23 answers
Defining methods via prototype vs using this in the constructor - really a performance difference?
6 answers
I am trying to comprehend the importance of Prototype Inheritance in Javascript
Consider this function Constructor
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Based on my vaguely understanding of how prototype works..
The JavaScript prototype property allows you to add new properties to object constructors:
So the following code
Person.prototype.nationality = "English";
This will probably change my function constructor behind the scene to something like this?
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English"
}
And hence whenever I create a new object, it will have access to nationality
var newPerson = new Person ("John", "Hapkins", "23", "black")
console.log(newPerson.nationality) //English
//To change nationality for just newPerson
newPerson.nationality = "German"
This is all cool, but I am unable to figure out the importance of prototype here?
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
javascript
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 13:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How does JavaScript .prototype work?
23 answers
Defining methods via prototype vs using this in the constructor - really a performance difference?
6 answers
I am trying to comprehend the importance of Prototype Inheritance in Javascript
Consider this function Constructor
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Based on my vaguely understanding of how prototype works..
The JavaScript prototype property allows you to add new properties to object constructors:
So the following code
Person.prototype.nationality = "English";
This will probably change my function constructor behind the scene to something like this?
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English"
}
And hence whenever I create a new object, it will have access to nationality
var newPerson = new Person ("John", "Hapkins", "23", "black")
console.log(newPerson.nationality) //English
//To change nationality for just newPerson
newPerson.nationality = "German"
This is all cool, but I am unable to figure out the importance of prototype here?
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
javascript
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 13:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?
– briosheje
Nov 22 at 13:40
1
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15
add a comment |
This question already has an answer here:
How does JavaScript .prototype work?
23 answers
Defining methods via prototype vs using this in the constructor - really a performance difference?
6 answers
I am trying to comprehend the importance of Prototype Inheritance in Javascript
Consider this function Constructor
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Based on my vaguely understanding of how prototype works..
The JavaScript prototype property allows you to add new properties to object constructors:
So the following code
Person.prototype.nationality = "English";
This will probably change my function constructor behind the scene to something like this?
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English"
}
And hence whenever I create a new object, it will have access to nationality
var newPerson = new Person ("John", "Hapkins", "23", "black")
console.log(newPerson.nationality) //English
//To change nationality for just newPerson
newPerson.nationality = "German"
This is all cool, but I am unable to figure out the importance of prototype here?
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
javascript
This question already has an answer here:
How does JavaScript .prototype work?
23 answers
Defining methods via prototype vs using this in the constructor - really a performance difference?
6 answers
I am trying to comprehend the importance of Prototype Inheritance in Javascript
Consider this function Constructor
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Based on my vaguely understanding of how prototype works..
The JavaScript prototype property allows you to add new properties to object constructors:
So the following code
Person.prototype.nationality = "English";
This will probably change my function constructor behind the scene to something like this?
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English"
}
And hence whenever I create a new object, it will have access to nationality
var newPerson = new Person ("John", "Hapkins", "23", "black")
console.log(newPerson.nationality) //English
//To change nationality for just newPerson
newPerson.nationality = "German"
This is all cool, but I am unable to figure out the importance of prototype here?
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
This question already has an answer here:
How does JavaScript .prototype work?
23 answers
Defining methods via prototype vs using this in the constructor - really a performance difference?
6 answers
javascript
javascript
asked Nov 22 at 13:34
NoobieSatan
1,045525
1,045525
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 13:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 13:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?
– briosheje
Nov 22 at 13:40
1
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15
add a comment |
1
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?
– briosheje
Nov 22 at 13:40
1
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15
1
1
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?– briosheje
Nov 22 at 13:40
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?– briosheje
Nov 22 at 13:40
1
1
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
Like wouldn't it be a better practise if we just add the new properties directly where we declare function constructor? instead of adding them using prototype?
So, what if you don't have access to the function constructor because the original code is not your but you can read it?– briosheje
Nov 22 at 13:40
1
1) No, that is not how the prototype works, see the duplicate. 2) Adding a single string property would be an unusual use of the prototype; typically you define shared functions (methods) on the prototype, which is much more efficient than defining individual functions on each individual object.
– deceze♦
Nov 22 at 13:42
@briosheje Interesting but In case orignal code is ours? like we aren't using any library?
– NoobieSatan
Nov 22 at 13:46
@deceze checking it out, thanks for sharing.
– NoobieSatan
Nov 22 at 13:47
@KuchBhi in case the original code is yours, there are some little performance tweaks you can do. Check the duplicate post to get further infos.
– briosheje
Nov 22 at 14:15