JS原型与继承

版权声明:转载请注明出处 https://blog.csdn.net/nk1212582/article/details/81321679

版权声明:转载请注明出处 https://blog.csdn.net/nk1212582/article/details/81321679

原型

每一个JS对象都是从一个原型继承而来的,可以通过其prototype属性获得该原型对象

JS对象继承机制建立在原型模型的基础之上

不仅可以使用原型扩展对象函数,还可以扩展对象的属性

function Vector(v1, v2){
	this.vec1 = v1;
	this.vec2 = v2;

	this.toString = function(){
		return "vec1 = " + this.vec1 + ", vec2 = " + this.vec2;
	}
}

Vector.prototype.sub = function(vector){
	this.vec1 = this.vec1 - vector.vec1;
	this.vec2 = this.vec2 - vector.vec2;	
}

var vecA = new Vector(10, 7);
var vecB = new Vector(1, 2);
vecA.sub(vecB)
console.log(vecA.toString())	//输出为:vec1 = 9, vec2 = 5

Cocos2d-x JS API的继承

所有类都直接或者间接继承自Class

(function () {
    var initializing = false, fnTest = /xyz/.test(function () {
        xyz;
    }) ? /\b_super\b/ : /.*/;

    // The base Class implementation (does nothing)
    this.Class = function () {
    };

    // Create a new Class that inherits from this class
    Class.extend = function (prop) {
        var _super = this.prototype;

        // Instantiate a base class (but only create the instance,
        // don't run the init constructor)
        initializing = true;
        var prototype = new this();
        initializing = false;

        // Copy the properties over onto the new prototype
        for (var name in prop) {
            // Check if we're overwriting an existing function
            prototype[name] = typeof prop[name] == "function" &&
                typeof _super[name] == "function" && fnTest.test(prop[name]) ?
                (function (name, fn) {
                    return function () {
                        var tmp = this._super;

                        // Add a new ._super() method that is the same method
                        // but on the super-class
                        this._super = _super[name];

                        // The method only need to be bound temporarily, so we
                        // remove it when we're done executing
                        var ret = fn.apply(this, arguments);
                        this._super = tmp;

                        return ret;
                    };
                })(name, prop[name]) :
                prop[name];
        }

        // The dummy class constructor
        function Class() {
            // All construction is actually done in the init method
            if (!initializing && this.init)
                this.init.apply(this, arguments);
        }

        // Populate our constructed prototype object
        Class.prototype = prototype;

        // Enforce the constructor to be what we expect
        Class.prototype.constructor = Class;

        // And make this class extendable
        Class.extend = arguments.callee;

        return Class;
    };
})();

//////////////////测试//////////////////

var Person = Class.extend({		//Class.extend表示继承自Class
	init: function(name){		//定义构造函数,其作用是初始化属性
		this.name = name;
	},
	getName: function(){		//定义普通函数
		return this.name;
	}
});

var Ninja = Person.extend({
	init: function(){
		this._super("忍者");	//调用父类的构造函数初始化父类的属性
	},
	getName: function(){		//重写getName()函数,它会覆盖父类的getName()函数
		return this._super();	//this._super()是掉用父类的getName()函数
	},
	swingSword: function(){		//子类新添加的函数
		return "舞剑";
	}
});

var p = new Person("普通人");
console.log(p.getName());	    //输出为:普通人

var n = new Ninja();
console.log(n.getName());	    //输出为:忍者
console.log(n.swingSword());    //输出为:舞剑

以上代码实现了一般意义上的面向对象的继承和多态机制

其中测试部分以上的代码是John Resiq所写,暂时不用管,复制粘贴即可

猜你喜欢

转载自blog.csdn.net/nk1212582/article/details/81321679