I am trying to achieve inheritance of this code snippet
function parent(name) { this.name = name; } function child(childname) { parent.call(this, "cde"); this.childname = childname; } var p1 = new parent("abc"); child.prototype = Object.create(parent.prototype); var p2 = new child("def"); console.log(p1.name); console.log(p2.name);
Using OLOO pattern OLOO pattern
Here is my try for it
function person(name) { this.name = name; } var p1 = new person("abc"); var p2; p2 = Object.create(person); console.log(p2.name); p2.childname = "def";
While creating p2 object here im not sending name to it. How can send name to it while creating p2 object .
Can some one please explain is this the right approach to achieve inheritance using OLOO pattern
Thanks
Both the current answers posted here are "correct" in that they appropriately apply object-only prototype linkage (without constructors). I would make two points of clarification, though:
I would never recommend "shadowing" of properties (or methods, for that matter), meaning having both p1
and p2
objects having a .name
property on them. This is one of the most common sources of confusion, IME, with JS prototypes. I always recommend using different property names on each object in a prototype chain.
OLOO is not just about wiring up a traditional "inheritance" hierarchy without using constructors, as the OP appears to be doing. That's valid, but a very reduced, simplified view of OLOO. OLOO is really intended to build off that capability to model software differently than with "classes". The point of OLOO is so that you set up two peer objects where one can delegate to the other (and vice versa, if you like) so they can virtually compose during the call-time of a method; aka: Behavior Delegation. Consult Chapter 6 of my "YDKJS: this & Object Prototypes" book for more info.
There are some misconceptions both in the question and answer, so I feel the need to clarify first a few things:
new
or Object.create()
it's all about linking . In your case, what you are trying to do is to link two objects so that one works as a "fallback" for the other (through the [[prototype]]
mecanism). And I repeat, this is NOT inheritance. In the above snippet, it's tempting to think that Foo is a "constructor", because we call it with new and we observe that it "constructs" an object.
In reality, Foo is no more a "constructor" than any other function in your program. Functions themselves are not constructors. However, when you put the new keyword in front of a normal function call, that makes that function call a "constructor call". In fact, new sort of hijacks any normal function and calls it in a fashion that constructs an object, in addition to whatever else it was going to do.
Finally, here is my answer (though it is not easy to understand what you really want to achieve...):
// a "parent" object with an "init" function let Parent = { define: function (name) { this.name = name; } }; // a child object with another "init" function let Child = Object.create(Parent); // different name for init() : useful to differentiate with Parent Child.create = function (childname) { this.define("cde"); this.childname = childname; }; let p1 = Object.create(Parent); p1.define("abc"); let p2 = Object.create(Child); p2.create("def"); console.log(p1.name); console.log(p2.name);
Using OLOO, you'd go for
var p1 = Object.create(Object.prototype);
p1.name = "abc";
var p2 = Object.create(p1);
p2.name = "cde";
p2.childname = "def";
or if you prefer
var p1 = {
name: "abc"
};
var p2 = Object.assign(Object.create(p1), {
name: "cde",
childname: "def"
});
Notice that OLOO does absolutely not use constructors. If you want to avoid repetition, use factory function that simply return the above expressions and are parameterised to your liking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.