javascript 中的继承方法

1.可以通过prototype属性,实现继承方法的方式,这种方式就是java语言中继承的变换形式。

 // Create the constructor for a Person object
function Person( name ) {
    this.name = name;
}

// Add a new method to the Person object
Person.prototype.getName = function() {
    return this.name;
};
Person.prototype.setName = function(name) {
    this.name = name;
};

// Create a new User object constructor
function User( name, password ) {
    // Notice that this does not support graceful overloading/inheritance
    // e.g. being able to call the super class constructor
    this.name = name;
    this.password = password;
};

// The User object inherits all of the Person object's methods
User.prototype = new Person();

 

2.通过自己写继承函数,实现继承,下面这段代码也很好,值得一看

// A simple helper that allows you to bind new functions to the
// prototype of an object
Function.prototype.method = function(name, func) {
    this.prototype[name] = func;
    return this;
};

// A (rather complex) function that allows you to gracefully inherit
// functions from other objects and be able to still call the  'parent'
// object's function
Function.method('inherits', function(parent) {
    // Keep track of how many parent-levels deep we are
    var depth = 0;

    // Inherit the parent's methods
    var proto = this.prototype = new parent();

    // Create a new 'priveledged' function called 'uber', that when called
    // executes any function that has been written over in the inheritance
    this.method('uber', function uber(name) {

        var func; // The function to be execute
        var ret; // The return value of the function
        var v = parent.prototype; // The parent's prototype

        // If we're already within another 'uber' function
        if (depth) {
            // Go the necessary depth to find the orignal prototype
            for ( var i = d; i > 0; i += 1 ) {
                v = v.constructor.prototype;
            }

            // and get the function from that prototype
            func = v[name];

        // Otherwise, this is the first 'uber' call
        } else {
            // Get the function to execute from the prototype
            func = proto[name];

            // If the function was a part of this prototype
            if ( func == this[name] ) {
                // Go to the parent's prototype instead
                func = v[name];
            }
        }

        // Keep track of how 'deep' we are in the inheritance stack
        depth += 1;

        // Call the function to execute with all the arguments but the first
        // (which holds the name of the function that we're executing)
        ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));

        // Reset the stack depth
        depth -= 1;

        // Return the return value of the execute function
        return ret;
    });

    return this;
});

// A function for inheriting only a couple functions from a parent object,
// not every function using new parent()
Function.method('swiss', function(parent) {
    // Go through all of the methods to inherit
    for (var i = 1; i < arguments.length; i += 1) {
        // The name of the method to import
        var name = arguments[i];

        // Import the method into this object's prototype
        this.prototype[name] = parent.prototype[name];
    }

    return this;
});

这段代码,我试过2层继承关系的,没任何问题,对于多层复杂性继承,还没试过

不过,可以看出作者功力很深,看了很开眼界。

这2段代码来自John Resig提功的 精通javascript 源代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值