-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Description
let sayMixin = {
say(phrase) {
alert(phrase);
}
};
let sayHiMixin = {
__proto__: sayMixin, // (or we could use Object.create to set the prototype here) !!! this may be wrong
sayHi() {
// call parent method
super.say(`Hello ${this.name}`); // (*)
},
sayBye() {
super.say(`Bye ${this.name}`); // (*)
}
};
class User {
constructor(name) {
this.name = name;
}
}
// copy the methods
Object.assign(User.prototype, sayHiMixin);
// now User can say hi
new User("Dude").sayHi(); // Hello Dude!
Object.create can set proto of sayHiMixin, but its method can be only function properties by
let sayHiMixin = Object.create(sayMixin, {
sayHi: {
value: function() {
super.say(`Hello ${this.name}`);
},
enumerable: true
}
});
or
sayHiMixin.sayHi = function() {
// ....
}
Calling super later will cause an error becasue both in these way there;s no [[HomeObject]], super keyword unexpected here.
Metadata
Metadata
Assignees
Labels
No labels