javascript - 'this' and 'prototype', simple function -
i trying resolve problem need understand prototype.
i reading , thought got it, still having complications
function person(name){ this.name = name; } person.prototype.greet = function(othername){ return "hi " + othername + ", name " + name; } var kate = new person('kate'); //name var jose = new person('jose'); //othername ???
so, mistake when need call function ? or ?
the name
property of object instance, need use this.name
in greet
method.
from understand, need display hi jose, name kate
greeting. in case need pass other person
greet
method can access persons name using object.name
function person(name) { this.name = name; } person.prototype.greet = function(other) { return "hi " + other.name + ", name " + this.name; } var kate = new person('kate'); var jose = new person('jose'); snippet.log(kate.greet(jose));
<!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Comments
Post a Comment