모방mootools 대상 창설 방법 간단한 실현

1612 단어 mootools
    var extend = function(destination, source, override) {

        if (override === undefined) override = true;

        for (var property in source) {

            if (!override && typeof(source[property]) == 'function' && destination.hasOwnProperty(property)) {

                destination[property] = (function(name, method) {

                    return function() {

                        this.base = source[name];

                        return method.apply(this, arguments);

                    }

                })(property, destination[property]);

            } else {

                destination[property] = source[property];

            }

        }



        return destination;

    };



    var Class = function(implements) {

        if (typeof implements == 'undefined') return;



        var newClass = function() {

            extend(this, implements);

            this.initialize.apply(this, arguments);

        };



        newClass.prototype.setOptions = function(opts) {

            extend(this.options, opts || {});

            return this;

        };



        return newClass;

    };



    var Tab = new Class({

        options : {

            name : 'test'

        },

        initialize : function(opts) {

            this.setOptions(opts);

            alert(this.options.name);

        },

        show : function() {

            return 'i will show';

        }

    });



    var t = new Tab({ name : 'newName' });

    alert(t.show());


  
여러분의 개선 의견을 환영합니다!

좋은 웹페이지 즐겨찾기