In traditional object-oriented programming languages, we provide a special syntax for subclasses to access the parent class, and we often need the extra support of the parent class method in implementing the subclass method. In this case, the subclass usually invokes the same method in the parent class, and eventually completes the work.
JavaScript does not have a special syntax like the one above, but we can create a!
function her () {};
Her.prototype.name = ' Anna ';
her.prototype.toString = function () {
var const = This.constructor;
Return const.uber? This.const.uber.toString () + ', ' + this.name:this.name;
}
function his () {};
var F = function () {};
F.prototype = Her.prototype;
His.prototype = new F ();
His.prototype.constructor = her;
His.uber = Her.prototype;
His.prototype.name = ' Jock ';
function child (width, height) {
this.width = width;
this.height = height;
}
var F = function () {};
F.prototype = His.prototype;
Child.prototype = new F ();
Child.prototype.constructor = child;
Child.uber = His.prototype;
Child.prototype.name = ' Los ';
Child.prototype.getArea = function () {return
this.width * this.height;
}
In the process of building a relationship, we introduced a Uber attribute and made it point to the parent and object.
Here, we have updated the following:
1. Set the Usber property to a reference to the parent object;
2. The ToString () method was updated;
The previous ToString () method simply returned this.name, and now we have added an extra task to check the This.constructor.usber property and call the ToString () method of the property if it exists.
Since This.constructor itself is a function, and This.constructo.usber is a reference to the current object's parent prototype, we call the ToString () method of the child entity, the ToString () on its prototype chain. Method will be invoked.
var i = child (1,2);
My.tostring ()//Anna, Jock, Los
The above is a small set to introduce the JavaScript neutron object access to the parent object of the way, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!