Per this answer (https://stackoverflow.com/a/28281845/1477388) I understand I can create a javascript object with it's own properties and methods like so.
However, when attempting to assign a particular method via a callback, it doesn't work!
Why can't I simply pass the method instead of passing the entire object?
JS Fiddle: https://jsfiddle.net/Lson3qwx/1/
// base `Person` prototype
var Person = {
name : '',
age : 22,
type : 'human',
msg: function(message) {
alert(message);
},
greet: function() {
//console.log('Hi, my name is ' + this.name + ' and I am a ' + this.type + '.' );
this.msg('Hi, my name is ' + this.name + ' and I am a ' + this.type + '.');
}
};
// create an instance of `Person`:
var skywalker = Object.create(Person);
skywalker.name = 'Anakin Skywalker';
//skywalker.greet(); // 'Hi, my name is Anakin Skywalker and I am a human.'
var callbackTest1 = function(callback) {
callback.greet();
}
var callbackTest2 = function(callback) {
callback();
}
callbackTest1(skywalker); // this works
callbackTest2(skywalker.greet); // this doesn't