JavaScript is implemented in a class inheritance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.