JavaScript prototype Tutorial Add Object Method and Property to Class

preview_player
Показать описание
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.
Рекомендации по теме
Комментарии
Автор

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.

JohnSmith-rnvl
Автор

I stopped learning JS because I was looking for the use of "prototype".

You just summed it in this video; thanks, Adam.

abkmf
Автор

i was searched videos to learn ptotoype base javascript in general.Now i got it .Thanks adam

SamithaHewawasam
Автор

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" :}

pauloteixeira
Автор

Спасибо! Великолепное видео. Сравнив с книгой носорога (стр.165 - 168) - я понял, что не посмотрев это видео, я бы скорее запутался в написанном, чем понял что-нибудь. Еще раз спасибо!

iarmolatii
Автор

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. 

coxthefox
Автор

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.

abhikgupta
Автор

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.

Автор

Wow, this is cool. Thanks again for the info!

McGavel
Автор

Very helpful, many thanks and please keep up the good work 

qkhader
Автор

Good tutorial. Just a question: Why not (in this context) just put heal-method and energy-property directly in the Player-class?

tomascarlsson
Автор

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

ga
Автор

It would be fascinating to see how this is setup in memory. Is there an object that gets added to every time you use the prototype functionality?

zissler
Автор

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?

PeterKassenaar
Автор

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

sarahjeffry
Автор

Coz u can add functions using prototype to all JS object, even this inside engine. Like String.

przemysawl.
Автор

hi im new to javascript. my question is why did u not place any parameters on function Player() is it consider good practice?

claracarbin
Автор

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??

bigtnt
Автор

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?

thecreativemastermin
Автор

You should only have methods in the prototype, not varibles. Changing the energy variable will affect all the instances of Players

christianmartin