The JavaScript class is the default prototype object inheritance:
var person = function () { this.name = "people"; This.hello = function () { console.log ("Hello User:" + this.name);} } var user = function () { this.name = "user"; This.hello = function () { User.prototype.hello.call (this, arguments); Console.log ("Hello User:" + this.name);} } User.prototype = new Person (); new User (). hello ();
There is no one way. You can make JavaScript classes like Java, calling the parent class through a superkeyword, so I extend the function:
(function () {Function.prototype.extend = function (BaseClass) {//This is a function object. var oldprototype = This.prototype, Newprototype = {}, _super = new BaseClass (); Inherits all properties and methods. for (var name in _super) {Newprototype[name] = _super[name]; } for (var name in oldprototype) {//* Override properties in this new Class. if (Oldprototype.hasownproperty (name)) {//Only function need _super. if (typeof oldprototype[name] = = "function" && typeof _super[name] = = "function") {Newprototyp E[name] = (function (name, FN) {return function () {var tmp = t His._super; Set Super Method This._super = _super[name]; var ret = fn.apply (this, arguments); This._super = tmp; return ret; }; }) (name, Oldprototype[name]); }}} This.prototype = Newprototype; return this; };}) ();
Test code
var A = function () { This.hello = function () { console.log ("Hello, I ' m A");} }; var B = function () {}; B.prototype = { hello:function () { this._super (); Console.log ("Hello, I ' m B"); }; B.extend (A); var C = function () {}; C.prototype = { hello:function () { this._super (); Console.log ("Hello, I ' m C"); }; C.extend (b); var B = new B (); var c = new C ();//b.hello (); C.hello ();
Results
Hello, i ' m ahello, I ' M bhello, I ' m C
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
JavaScript is implemented in a class inheritance