JavaScript prototype Tutorial Add Object Method and Property to Class




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”

  1. 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

  2. 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);

  3. 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);

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

  5. 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.

  6. 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. 

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

  8. 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.

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

Leave a Reply