I'm still trying to get to grips with how this JS inheritance stuff works and each time I think I have it... I clearly don't.
Attempt 1
link: https://jsfiddle.net/jacquesvanderhoven/4qkvdn46/
Code
var baseSettings = function ()
{
this.message = "base message";
this.getMessage = function(){
return "base message from function";
}
};
var homeSettings = function ()
{
this.name = "name";
this.getName = function()
{
return "name from function";
};
};
homeSettings.prototype = Object.create(baseSettings);
var $log = $("#log");
var base = new baseSettings();
var home = new homeSettings();
$log.html($log.html() + "<br /> baseSettings.message: '" + base.message + "'");
$log.html($log.html() + "<br /> baseSettings.getMessage(): '" + base.getMessage() + "'");
$log.html($log.html() + "<br /> homeSettings.name: '" + home.name + "'");
$log.html($log.html() + "<br /> homeSettings.getName(): '" + home.getName() + "'");
$log.html($log.html() + "<br /> homeSettings.message: '" + home.message + "'");
$log.html($log.html() + "<br /> homeSettings.getMessage(): '" + home.getMessage() + "'");
Results
baseSettings.message: 'base message'
baseSettings.getMessage(): 'base message from function'
homeSettings.name: ''
homeSettings.getName(): 'name from function'
homeSettings.message: 'undefined'
Two of these actually throw JS exceptions.
Attemp 2
If I add a call to the super constructor it changes things a bit:
Link: https://jsfiddle.net/jacquesvanderhoven/4qkvdn46/5/
Code
var baseSettings = function ()
{
this.message = "base message";
this.getMessage = function(){
return "base message from function";
}
};
var homeSettings = function ()
{
baseSettings.call(this);
this.name = "name";
this.getName = function()
{
return "name from function";
};
};
homeSettings.prototype = Object.create(baseSettings);
var $log = $("#log");
var base = new baseSettings();
var home = new homeSettings();
$log.html($log.html() + "<br /> baseSettings.message: '" + base.message + "'");
$log.html($log.html() + "<br /> baseSettings.getMessage(): '" + base.getMessage() + "'");
$log.html($log.html() + "<br /> homeSettings.name: '" + home.name + "'");
$log.html($log.html() + "<br /> homeSettings.getName(): '" + home.getName() + "'");
$log.html($log.html() + "<br /> homeSettings.message: '" + home.message + "'");
$log.html($log.html() + "<br /> homeSettings.getMessage(): '" + home.getMessage() + "'");
Results
baseSettings.message: 'base message'
baseSettings.getMessage(): 'base message from function'
homeSettings.name: '' (Note! This is empty and shouldn't be)
homeSettings.getName(): 'name from function'
homeSettings.message: 'base message'
homeSettings.getMessage(): 'base message from function'
Most of them work, but I can't figure out why homeSettings.name returns nothing?
It's starting to look to me like Object.create does nothing really, or rather it requires the call to the super-constructor to make any difference, but even then it's not perfect.
Attempt 3
Remove the call to Object.create and leave the call to the super constructor.
Link: https://jsfiddle.net/jacquesvanderhoven/4qkvdn46/8/
Code
var baseSettings = function ()
{
this.message = "base message";
this.getMessage = function(){
return "base message from function";
}
};
var homeSettings = function ()
{
baseSettings.call(this);
this.name = "name";
this.getName = function()
{
return "name from function";
};
};
var $log = $("#log");
var base = new baseSettings();
var home = new homeSettings();
$log.html($log.html() + "<br /> baseSettings.message: '" + base.message + "'");
$log.html($log.html() + "<br /> baseSettings.getMessage(): '" + base.getMessage() + "'");
$log.html($log.html() + "<br /> homeSettings.name: '" + home.name + "'");
$log.html($log.html() + "<br /> homeSettings.getName(): '" + home.getName() + "'");
$log.html($log.html() + "<br /> homeSettings.message: '" + home.message + "'");
$log.html($log.html() + "<br /> homeSettings.getMessage(): '" + home.getMessage() + "'");
Results
baseSettings.message: 'base message'
baseSettings.getMessage(): 'base message from function'
homeSettings.name: 'name'
homeSettings.getName(): 'name from function'
homeSettings.message: 'base message'
homeSettings.getMessage(): 'base message from function'
Conclusion
I'm working through various examples I'm seeing on SO and I have to admit I'm not getting to a point where I understand why inheritance works or doesn't work in JS. Another problem I'm having is that in various tests the context for 'this' is changed, which I know happens, but I can't seem to get a good understanding of why or when so that I know exactly what's going on.
Object.create()is not really wrong, but it's strange and it won't work like you think. A constructor function is just a function; a prototype object is a plain object with properties that provide shared functionality, and generally that's what you pass toObject.create().this.getMessage = functionsets thegetMessagefor each instance of the object individually on creation time. What you are looking for is to setbaseSettings.prototype.getMessage = functionoutside of the constructor.