使用webstorm测试javascript继承

使用webstorm测试javascript继承
// Inspired by base2 and Prototype
(function(){
    var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

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

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

        console.log("Test : " + self == this);
        // 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
            var TEstB = "AFBF";
            prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
                (function(name, fn){
                    return function() {
                        var tmp = this._super;
                        console.log("Teddd : " +TEstB);
                        // 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 DummyTest() {
            // All construction is actually done in the init method
            if ( !initializing && this.init )
                this.init.apply(this, arguments);
        }

        // Populate our constructed prototype object 子类的prototype指向父类的实例(完成继承)
        DummyTest.prototype = prototype;

        // Enforce the constructor to be what we expect  修正constructor指向
        DummyTest.prototype.constructor = DummyTest;
        // And make this class extendable // 子类自动获取extend方法,arguments.callee指向当前正在执行的函数
        DummyTest.extend = arguments.callee;

        return DummyTest;
    };

})();

var Person = TestExend.extend({
    init: function(isDancing){
        this.dancing = isDancing;
    }
});

var Ninja = Person.extend({
    init: function(){
        this._super( false );
    }
});
var p = new Person(true);
p.dancing; // => true
var n = new Ninja();
n.dancing; // => false

var AAA = function(){
    this.varial = "Test static";
    console.log("AAA");};
AAA.testA = function(){
    new this();
    console.log("bbb " + this.varial);};

var BBB = new AAA();
AAA.testA();
//BBB.testA();

猜你喜欢

转载自blog.csdn.net/GJQI12/article/details/80266695