JavaScript Constructor Is Not Executed?
Solution 1:
In you case var Jill acts like a regular function, not like constructor.
Change your code as shown below:
var Jill = (function () {
function Jill() {
Person.call(this);
this.name = "Jill";
};
Jill.prototype = Object.create(Person.prototype);
Jill.prototype.constructor = Jill;
Jill.prototype.expressJoy = function () {
console.log("Huray!");
};
return Jill;
})();
var jill = new Jill();
console.log(jill); // Jill {name: "Jill"}
console.log(jill instanceof Jill); // true
jill.expressJoy(); // Huray!
jill.sayName(); // My name is Jill
Now Jill is a "real" constructor that will generate objects as you expected. (BTW, A constructor name should start with an uppercase letter, according to good practice)
Solution 2:
You are wrongly initiating the object,
var jill = Jill();
var obj = new jill();
The function Jill is returning a function reference not an object. And on top of that, returning function reference we have to create the object for your requirement.
Solution 3:
This is the corrected code, will edit with detailed explanation:
// This is your Person class, all "people" will inherit from this object
var Person = function() {
this.name = "unnamed";
};
// add the sayName function to this prototype
Person.prototype.sayName = function() {
console.log("My name is " + this.name);
};
// now we make a 'Jill' constructor;
var Jill = function() {
Person.call(this); // bind this (Jill) to Person
this.name = 'jill';
};
// create the correct prototype delegation here
Jill.prototype = Object.create(Person.prototype);
// the above code, sets Jill's constructor, to Person so we need to set
// it back to Jill here:
Jill.prototype.constructor = Jill;
// add functions on Jill's prototype, these will only be on the Jill object not Person
Jill.prototype.expressJoy = function() {
console.log("Huray!");
};
// make a 'Jill'
var obj = new Jill();
console.log(obj instanceof Jill); // true
obj.expressJoy(); // "Huray!"
obj.sayName(); // "My name is jill"
Now more importantly you are mixing two types of instantiation patterns, prototypal and pseudoclassical, Person is the latter, while Jill is the former.
Take a look at this awesome picture from the following blogpost by Ryan Atkinson:
Solution 4:
With your code you need to:
1) return new jill(); in the inner function, and then
2) var jill = Jill(); to call the function that creates the new instance.
This is a less-complicated method.
function Person() {
this.name = "unnamed";
}
Person.prototype.sayName = function() {
console.log("My name is " + this.name);
}
function Jill() {
Person.call(this);
this.name = "Jill";
}
Jill.prototype = Object.create(Person.prototype);
Jill.prototype.constructor = Jill;
Jill.prototype.expressJoy = function() {
console.log("Huray!");
};
var jill = new Jill();
Solution 5:
This should work
function Person() {
this.name = "unnamed";
};
Person.prototype.sayName = function() {
console.log("My name is " + this.name);
};
function Jill() {
Jill.prototype.__proto__ = Person.prototype;
this.name = "Jill";
Jill.prototype.expressJoy = function() {
console.log("Huray!");
};
return Jill;
};
var jill = new Jill();
console.log(jill instanceof Jill); // True
jill.expressJoy(); // Hurray !
jill.sayName(); // My name is Jill

Post a Comment for "JavaScript Constructor Is Not Executed?"