Lesson Code: http://www.developphp.com/video/JavaScript/prototype-Tutorial-Add-Object-Methods-and-Properties-to-Class
The JavaScript prototype property allows us to add specialized methods and properties to our custom objects and classes. There may be occasions when you must specialize the functionality of an object in your application, and the prototype property allows us to do that. It also enables Object Inheritance in JavaScript, which we will be demonstrating in the video that follows this one. Before we discuss Object Inheritance let’s write some code that demonstrates how the prototype property works at a basic level in JavaScript.
Original source
50 responses to “JavaScript prototype Tutorial Add Object Method and Property to Class”
Adam, in the example you illustrating you used the same name which makes things confusing like
this.ATTACK = function ATTACK(opponent) or
Player.prototype. HEAL = function HEAL(targetPlayer)
when accessing the heal method which one are we call the one before the "=" or the one after it that proceeded by the function keyword
Thanks
Thanks for the video!!
aaaaaaaaaaand subscribed.
What's the benefit of adding a property with prototype instead of adding it straight to the class?
You should have used kratos instead of conan.
What is the benefit of using prototypes ?when we can easily do this like this…
var player = function(){
this.name;
this.hitpoints = 100;
this.attack = function (opp){
opp.hitpoints -= 10;}
this.heal = function(){
this.hitpoints+= 5;
}
}
var p1 = new player();
var p2 = new player();
p1.name= 'MAJID';
p2.name='KHASIM';
p1.attack(p2);
console.log(p2.name+ " " +p2.hitpoints);
p2.heal();
console.log(p2.name+ " " +p2.hitpoints);
What is the benefit of using prototypes ?when we can easily do this like this…
var player = function(){
this.name;
this.hitpoints = 100;
this.attack = function (opp){
opp.hitpoints -= 10;}
this.heal = function(){
this.hitpoints+= 5;
}
}
var p1 = new player();
var p2 = new player();
p1.name= 'MAJID';
p2.name='KHASIM';
p1.attack(p2);
console.log(p2.name+ " " +p2.hitpoints);
p2.heal();
console.log(p2.name+ " " +p2.hitpoints);
i love you man! very helpful
NICE
hi im new to javascript. my question is why did u not place any parameters on function Player() is it consider good practice?
why you did this and not var? cuz u didn't want global or? thax@!
Didn't think javascript had classes?
so prototype is just adding
this.heal
in function Player(){}??
Good tutorial. Just a question: Why not (in this context) just put heal-method and energy-property directly in the Player-class?
thx! very helpfull
Thank you, you are very clear.
you sir are a very good teacher
Спасибо! Великолепное видео. Сравнив с книгой носорога (стр.165 – 168) – я понял, что не посмотрев это видео, я бы скорее запутался в написанном, чем понял что-нибудь. Еще раз спасибо!
awesomesauce
Thank you!
Great explanation, Thanks!
good example
Wow, this is cool. Thanks again for the info!
He keeps saying specialize, but what that mean?
Nice tutorial.
I think you don't need to repeat the function's name as you did, just write:
Player.prototype.heal = function(targetPlayer) { … } instead:
Player.prototype.heal = function heal(targetPlayer) { … }
Regards.
You should only have methods in the prototype, not varibles. Changing the energy variable will affect all the instances of Players
thank you…very good example
Great explanation
Hi Adam – great video. Really clear. I'm trying to learn Javacsript using video tutorials and I have paid to access a couple of sites, but I have to say, I think your videos are better than then the paid-for content I've been seeing. Great help.
I cann't understand why we should define Player as function no a var object ?
Can any one explain it please ? It is really appreciated
I don´t believe Conan could beat Hercules… Hercules is a Semi-God and Conan is just a barbarian. A very strong barbarian but still a man. Hercules is a son of Zeus, the most powerful of Olympus Gods. I don´t care if javascript is great(sneaky… thing, we know you´re called "liveScript" in the old days, You may change the name but we never forget a… sintax), We don´t care that "this.prototype.heal" as great strength… to us this.story is just a "cheat.property" and "this.game" sucks ;).
Keep up the good work guys, "this.videos" are very useful.{ "Thanks.alot" :}
Adam – I have a question. Why use prototype property to extend an object when we can simply extend it by using object.newProperty = newPropValue syntax?
nice
Gr8 video!
Great tutorials! Thank you very much. What IDE are you using in this tutorial? Is it free?
very nice thank you
Awesome tutorials!
awesome video helps me a lot
Ok I understand how to use the object.prototype.function() but, how is it different from than just defining the method in the Player() object??
I spent a couple hours a day for 3 days trying to get my head around this prototype stuff from different sources, yours is the best explanation I have seen. Thank you.
excellent!, easy to understand Adam!
Very helpful, many thanks and please keep up the good work
What if your player gained a level and "unlocked" a new skill.. You would have to give the player a new skill. aka heal
The basic usage of the prototype property is for inheritance and sometimes, for dynamically injecting properties/ functionalities into a Javascript object on the fly. What if you want a property based on a conditional statement? It is sort of like reflection and I get it why most people think of it as a hack but it is really useful once the requirements keep flowing in.
answer the questions adam please….
Coz u can add functions using prototype to all JS object, even this inside engine. Like String.
I get it. But WHY would you want to add functionality using prototype. Why don't you simply add it directly to the Player() class in the first place? Must be something really simple I'm missing, but working with prototype looks like a detour here?
Great tutorial Adam! Thank you for doing this!!
I stopped learning JS because I was looking for the use of "prototype".
You just summed it in this video; thanks, Adam.
Great video
You see stupid videos on youtube having more than one 1 m views,and educational videos like this that are not so popular. No wonder why the world is in crisis!!!